Skip to content

Instantly share code, notes, and snippets.

@ariwaranosai
Forked from Centaur/flatten.scala
Created December 8, 2015 05:51
Show Gist options
  • Save ariwaranosai/29f97d43f27e6c95d21d to your computer and use it in GitHub Desktop.
Save ariwaranosai/29f97d43f27e6c95d21d to your computer and use it in GitHub Desktop.
Shapeless tuple flatten
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