Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Created April 25, 2013 02:26
Show Gist options
  • Save travisbrown/5457070 to your computer and use it in GitHub Desktop.
Save travisbrown/5457070 to your computer and use it in GitHub Desktop.
import shapeless._
type StringTo[X] = String => X
case class StringFunctions[H <: HList](functions: H = HNil: HNil) {
def addFunction[T](f: StringTo[T]) = StringFunctions[StringTo[T] :: H](f :: functions)
def map[H1 <: HList, R](f: H1 => R)(implicit mapped: MappedAux[H1, StringTo, H]) = ???
def map2[T1, T2, R](f: (T1, T2) => R)(implicit ev: H <:< (StringTo[T1] :: StringTo[T2] :: _)) = ???
def apply[S <: HList](strings: S)(implicit zip: ZipApply[H, S]) = zip(functions, strings)
}
val f1 = (_: String).toInt
val f2 = (s: String) => s.toInt + 1
val sum2: (Int :: Int :: HNil) => Int = { case i :: j :: HNil => i + j }
val plus: (Int, Int) => Int = { (i, j) => i + j }
@travisbrown
Copy link
Author

And then the following compile:

StringFunctions().addFunction(f1).addFunction(f2).map(sum2)
StringFunctions().addFunction(f1).addFunction(f2).map2(plus)
StringFunctions().addFunction(f1).addFunction(f2).addFunction(f1).map2(plus)
StringFunctions().addFunction(f1).addFunction(f2)("12" :: "13" :: HNil)

But not these:

StringFunctions().addFunction(f1).map(sum2)
StringFunctions().addFunction(f1).map2(plus)
StringFunctions().addFunction(f1)("12" :: "13" :: HNil)

And so on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment