fork download
  1. case class Layering[+C[_]](fn: Any => C[Any]) {
  2. def apply(x: Any): C[Any] = fn(x)
  3. }
  4.  
  5. object Layering {
  6. val list = Layering[List](List(_))
  7.  
  8. def compose[C[_]](layerings: Seq[Layering[C]]) =
  9. layerings.reduce[Layering[C]] { case (l1, l2) =>
  10. Layering[C](x => l1.fn(l2.fn(x)))
  11. }
  12. }
  13.  
  14. object Main extends App {
  15. import Layering._
  16. val layerings = Seq(list, list)
  17. println(compose(layerings)(12))
  18. }
Success #stdin #stdout 0.41s 382080KB
stdin
Standard input is empty
stdout
List(List(12))