Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created December 8, 2015 05:47
Show Gist options
  • Save Centaur/052c37d112c85a3bf52d to your computer and use it in GitHub Desktop.
Save Centaur/052c37d112c85a3bf52d 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