Skip to content

Instantly share code, notes, and snippets.

@banshee
Last active February 11, 2016 01:41
Show Gist options
  • Save banshee/afc1d024849ddc1a0807 to your computer and use it in GitHub Desktop.
Save banshee/afc1d024849ddc1a0807 to your computer and use it in GitHub Desktop.
shapeless experiments
scala> import shapeless._
import shapeless._
scala> import shapeless.ops.function.FnToProduct
import shapeless.ops.function.FnToProduct
scala>
scala> // a normal function taking a string and returning 42
scala> def fn1(a: String) = 42
fn1: (a: String)Int
scala>
scala> /**
| * A function that takes a function that takes arguments and returns
| * a function taking a single HList.
| *
| * @param fn The function to convert. Notice that the type is just F; the heavy lifting is done by implicit resolution
| * @param fnToProduct The tool you use to convert functions taking arguments to functions taking HLists
| * @tparam F The type of the input function
| * @tparam L Used by FnToProduct.Aux
| * @tparam R Used by FnToProduct.Aux
| * @return
| */
| def convertToFunctionTakingHList[F, L <: HList, R](fn: F)(implicit fnToProduct: FnToProduct.Aux[F, L => R]): fnToProduct.Out = fnToProduct(fn)
convertToFunctionTakingHList: [F, L <: shapeless.HList, R](fn: F)(implicit fnToProduct: shapeless.ops.function.FnToProduct.Aux[F,L => R])fnToProduct.Out
scala>
scala> val fn2: FnToProduct.Aux[(String) => Int, (::[String, HNil]) => Int]#Out = convertToFunctionTakingHList(fn1 _)
fn2: shapeless.::[String,shapeless.HNil] => Int = <function1>
scala> val result = fn2("a string" :: HNil)
result: Int = 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment