-
-
Save milessabin/5123666 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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