Skip to content

Instantly share code, notes, and snippets.

@milessabin
Forked from puffnfresh/Fogus.scala
Last active December 14, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save milessabin/5123666 to your computer and use it in GitHub Desktop.
Save milessabin/5123666 to your computer and use it in GitHub Desktop.
import shapeless._
// fogus wanted this:
// [a] -> [b] -> [a b a b ...]
// Follow up question: "Do you know a dependent language with my interleave?"
// Yes. Scala and shapeless even make it a single line!
trait AB[-L <: HList, A, B]
object AB {
implicit def hnilAB[A, B] = new AB[HNil, A, B] {}
implicit def hconsAB[A, B, T <: HList](implicit abt: AB[T, B, A]) = new AB[A :: T, A, B] {}
}
object Fogus {
val a = 1 :: 2 :: 3 :: HNil
val b = "one" :: "two" :: "three" :: HNil
val interleaved = (a :: b :: HNil).transpose.flatMap(identity)
// 1 :: one :: 2 :: two :: 3 :: three :: HNil
// proof that interleaved alternates Int and Strings
implicitly[AB[interleaved.type, Int, String]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment