blob: e959619dd112a394dfba046271326a1cc854e368 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
trait Fun[A, B] { def apply(a: A): B }
class PF[A, B] { def runWith[U](action: Fun[B, U]): Fun[A, Boolean] = a => {action(a.asInstanceOf[B]); true} }
class TO[A](x: A) {
def foreach[U](f: Fun[A, U]): U = f(x)
def collectFirst[B](pf: PF[A, B]): Option[B] = {
foreach(pf.runWith(b => return Some(b)))
None
}
}
object Test extends App {
assert(new TO("a").collectFirst(new PF[String, String]).get == "a")
}
|