Suppose I have an HList
of type A::B::C::HNil
and a function (A, B, C) => D
val hlist: A::B::C::HNil = ???
def foo(a: A, b: B, c: C): D = ???
Now I need a funciton A::B::C::HNil => D
, which uses foo
to return D
.
def bar(hslist: A::B::C::HNil): D = ???
How would you implement bar
?
You can do this a little more directly using Shapeless's FnToProduct
, which provides toProduct
syntax for converting a FunctionN
into a Function1
taking an HList
:
import shapeless._, syntax.std.function._
type A = String
type B = Symbol
type C = Int
type D = List[String]
val hlist: A :: B :: C :: HNil = "foo" :: 'x :: 1 :: HNil
def foo(a: A, b: B, c: C): D = List.fill(c)(a + b)
def bar(hslist: A :: B :: C :: HNil): D = (foo _).toProduct.apply(hslist)
In many cases you probably wouldn't even want a separate bar
definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With