Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke function of multiple arguments with HList?

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 ?

like image 931
Michael Avatar asked Jul 23 '15 13:07

Michael


1 Answers

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.

like image 92
Travis Brown Avatar answered Nov 15 '22 07:11

Travis Brown