Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to yield a single element from for loop in scala?

Tags:

Much like this question:

Functional code for looping with early exit

Say the code is

def findFirst[T](objects: List[T]):T = {
  for (obj <- objects) {
    if (expensiveFunc(obj) != null) return /*???*/ Some(obj)
  }
  None
}

How to yield a single element from a for loop like this in scala?

I do not want to use find, as proposed in the original question, i am curious about if and how it could be implemented using the for loop.

* UPDATE *

First, thanks for all the comments, but i guess i was not clear in the question. I am shooting for something like this:

val seven = for {
    x <- 1 to 10
    if x == 7
} return x

And that does not compile. The two errors are: - return outside method definition - method main has return statement; needs result type

I know find() would be better in this case, i am just learning and exploring the language. And in a more complex case with several iterators, i think finding with for can actually be usefull.

Thanks commenters, i'll start a bounty to make up for the bad posing of the question :)

like image 293
Julio Faerman Avatar asked Nov 12 '12 12:11

Julio Faerman


People also ask

What is yield in for loop Scala?

yield keyword will returns a result after completing of loop iterations. The for loop used buffer internally to store iterated result and when finishing all iterations it yields the ultimate result from that buffer.

How do you break a loop in Scala?

In Scala, we use a break statement to break the execution of the loop in the program. Scala programing language does not contain any concept of break statement(in above 2.8 versions), instead of break statement, it provides a break method, which is used to break the execution of a program or a loop.

Which keyword is used to return value from for loop in Scala?

Using for-loop with Yield In Scala, the return value of the for loop is stored in a variable or may return through a function. To do this you should use yield keyword to prefix the body of for loop. Example: Scala.

What does => mean in Scala?

=> is the "function arrow". It is used both in function type signatures as well as anonymous function terms. () => Unit is a shorthand for Function0[Unit] , which is the type of functions which take no arguments and return nothing useful (like void in other languages).


1 Answers

If you want to use a for loop, which uses a nicer syntax than chained invocations of .find, .filter, etc., there is a neat trick. Instead of iterating over strict collections like list, iterate over lazy ones like iterators or streams. If you're starting with a strict collection, make it lazy with, e.g. .toIterator.

Let's see an example.

First let's define a "noisy" int, that will show us when it is invoked

def noisyInt(i : Int) = () => { println("Getting %d!".format(i)); i }

Now let's fill a list with some of these:

val l = List(1, 2, 3, 4).map(noisyInt)

We want to look for the first element which is even.

val r1 = for(e <- l; val v = e() ; if v % 2 == 0) yield v

The above line results in:

Getting 1!
Getting 2!
Getting 3!
Getting 4!
r1: List[Int] = List(2, 4)

...meaning that all elements were accessed. That makes sense, given that the resulting list contains all even numbers. Let's iterate over an iterator this time:

val r2 = (for(e <- l.toIterator; val v = e() ; if v % 2 == 0) yield v)

This results in:

Getting 1!
Getting 2!
r2: Iterator[Int] = non-empty iterator

Notice that the loop was executed only up to the point were it could figure out whether the result was an empty or non-empty iterator.

To get the first result, you can now simply call r2.next.

If you want a result of an Option type, use:

if(r2.hasNext) Some(r2.next) else None

Edit Your second example in this encoding is just:

val seven = (for {
    x <- (1 to 10).toIterator
    if x == 7
} yield x).next

...of course, you should be sure that there is always at least a solution if you're going to use .next. Alternatively, use headOption, defined for all Traversables, to get an Option[Int].

like image 50
Philippe Avatar answered Oct 06 '22 04:10

Philippe