How does this recursive List flattening work?
A while back this was asked and answered on the Scala mailing list: Kevin: Given some nested structure: List[List[...List[T]]] what's the best (preferably type-safe) way to flatten it to a List[T] Jesper: A combination of implicits and default arguments works: case class Flat[T, U](fn : T => List[U]) implicit def recFlattenFn[T, U](implicit f : Flat[T, U] = Flat((l : T) => List(l))) = Flat((l : List[T]) => l.flatMap(f.fn)) def recFlatten[T, U](l : List[T])(implicit f : Flat[List[T], U]) = f.fn(l) Examples: scala> recFlatten(List(1, 2, 3)) res0: List[Int] = List(1, 2, 3) scala> recFlatten(List