scala - Composing two functions to get a function that returns HList -
this naive question shapeless:
suppose have functions a => m[b] , b => m[c]. how can compose them new function a => m[b::c::hnil] ?
if want generically can use scalaz's arrow:
import scalaz._, scalaz._ def andthenbutkeep[arr[_, _]: arrow, a, b, c]( f: arr[a, b], g: arr[b, c] ): arr[a, (b, c)] = f >>> (category[arr].id &&& g) or if want hlist instead of tuple:
import scalaz._, scalaz._ import shapeless._, shapeless.syntax.std.tuple._ def andthenbutkeep[arr[_, _], a, b, c]( f: arr[a, b], g: arr[b, c] )(implicit arr: arrow[arr]): arr[a, b :: c :: hnil] = f >>> (arr.id &&& g) >>> arr.arr((_: (b, c)).productelements) now you'd wrap functions in kleisli arrow:
type optionfunc[a, b] = kleisli[option, a, b] val f: optionfunc[int, string] = kleisli(i => some("a" * i)) val g: optionfunc[string, int] = kleisli(s => some(s.length)) val c = andthenbutkeep(f, g) and then:
scala> println(c.run(10)) some(aaaaaaaaaa :: 10 :: hnil) you make little less fussy type inference (but less generic) restricting arrow kleisli arrow on m.
Comments
Post a Comment