- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
[Fwd: Re: Continuation on Tasks]
Sat, 2009-11-21, 21:17
Daniel Sobral schrieb:
> This is not pseudo-code, though there is one bug. It works on Scala 2.8.
> On Scala 2.7, you just need to convert view to projection, though there
> is a view differences between them.
>
> Here is one example:
>
> scala> class A(n: Int) { def process = { println(n); n } }
> defined class A
>
> scala> val p1 = new A(1).process _
> p1: () => Int =
>
> scala> val p2 = new A(2).process _
> p2: () => Int =
>
> scala> val p3 = new A(3).process _
> p3: () => Int =
>
> scala> val p4 = new A(4).process _
> p4: () => Int =
>
> scala> val processes = List(p1, p2, p3, p4)
> processes: List[() => Int] = List(, , ,
> )
>
> scala> def predicate(n: Int): Boolean = n % 2 == 0
> predicate: (Int)Boolean
>
> scala> def process[A](processes: List[() => A], predicate: (A) => Boolean) {
> | processes.projection map (_()) map predicate exists (result =>
> result)
> | }
> process: [A](List[() => A],(A) => Boolean)Unit
>
> scala> process(processes, predicate)
> 1
> 2
very neat, thanks for that.
I was used to haskell's lazy eval, I had no clue that you have to use
projection or view to get a non-strict behaviour.
thanks,
Kai