case class Layering
[+C
[_]](fn
: Any
=> C
[Any
]) { def apply
(x
: Any
): C
[Any
] = fn
(x
) }
val list
= Layering
[List
](List
(_))
def compose
[C
[_]](layerings
: Seq
[Layering
[C
]]) = layerings.
reduce[Layering
[C
]] { case (l1, l2
) => Layering[C](x => l1.fn(l2.fn(x)))
}
}
val layerings
= Seq
(list, list
) println(compose(layerings)(12))
}
Y2FzZSBjbGFzcyBMYXllcmluZ1srQ1tfXV0oZm46IEFueSA9PiBDW0FueV0pIHsKICBkZWYgYXBwbHkoeDogQW55KTogQ1tBbnldID0gZm4oeCkKfQoKb2JqZWN0IExheWVyaW5nIHsKICB2YWwgbGlzdCA9IExheWVyaW5nW0xpc3RdKExpc3QoXykpCgogIGRlZiBjb21wb3NlW0NbX11dKGxheWVyaW5nczogU2VxW0xheWVyaW5nW0NdXSkgPQogICAgbGF5ZXJpbmdzLnJlZHVjZVtMYXllcmluZ1tDXV0geyBjYXNlIChsMSwgbDIpID0+CiAgICAgIExheWVyaW5nW0NdKHggPT4gbDEuZm4obDIuZm4oeCkpKQogICAgfQp9CgpvYmplY3QgTWFpbiBleHRlbmRzIEFwcCB7CiAgaW1wb3J0IExheWVyaW5nLl8KICB2YWwgbGF5ZXJpbmdzID0gU2VxKGxpc3QsIGxpc3QpCiAgcHJpbnRsbihjb21wb3NlKGxheWVyaW5ncykoMTIpKQp9