Created
December 8, 2015 05:47
-
-
Save Centaur/052c37d112c85a3bf52d to your computer and use it in GitHub Desktop.
Shapeless tuple flatten
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._ | |
import poly._ | |
import shapeless.ops.tuple.FlatMapper | |
import syntax.std.tuple._ | |
object Flatten { | |
trait LowPriorityFlatten extends Poly1 { | |
implicit def default[T] = at[T](Tuple1(_)) | |
} | |
object flatten extends LowPriorityFlatten { | |
implicit def caseTuple[P <: Product](implicit fm: FlatMapper[P, flatten.type]) = | |
at[P](_.flatMap(flatten)) | |
} | |
def main(args: Array[String]) { | |
println(flatten((1, (2, (3, (4, 5))))).toList) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment