2009/6/4 Rajkumar Goel:

> Hi all,
> I'm not able to understand what would be the use of the yield in the for
> each expression provided by SCALA.
> some detail about what yield does and in what context we can use it would
> be really helpful.
> Thanks ,
> Rajkumar Goel


Hello Rajkumar,
consider this code from scala-lang.org:


def olderThan20(xs: Iterator[Person]): Iterator[String] = {

// The first expression is called a 'generator' and makes
// 'p' take values from 'xs'. The second expression is
// called a 'filter' and it is a boolean expression which
// selects only persons older than 20. There can be more than
// one generator and filter. The 'yield' expression is evaluated
// for each 'p' which satisfies the filters and used to assemble
// the resulting iterator

for (p <- xs if p.age > 20) yield p.name <javascript:void(0);>
}


***    yield will return an arbitrary collection depending on the specific
collection you passed on the right hand of <-



-- 
Cheers,
Pratik K Anand


[Non-text portions of this message have been removed]

Reply via email to