0

I can collect the results at the inner-most for body into a List[Output] and return them. But I want to use yield. How can this method be converted into using for-yield pattern:

def useForComprehension(input : Input): List[Output] = {        
  for (o <- splitInputIntoPieces(input)) {
    for (restResults <- useForComprehension(subtract(input, o)) ) {
        for (w <- f3(o)) {
          yield w::restResults // !!!!! Error
        }
    }           
  }    
}
Asad Iqbal
  • 3,241
  • 4
  • 32
  • 52
  • [I'm getting entirely different error](http://scalafiddle.net/console/4e38d30e656da5ae9d3a425109ce9e04), which makes me think your example is wrong (the code that shows your problem is other than in that in question) or you have quite old scala version, because in recent scala versions you can not have `yield` inside of for's `{}` (it must be the very first statement) – om-nom-nom Nov 02 '14 at 04:58
  • I am learning the use of yield. How could I accomplish the above functionality (although the code does not compile, can you get my intention? ) using `yield` correctly? – Asad Iqbal Nov 02 '14 at 15:27
  • write `yield { ... }` not `{ yield ...}` – om-nom-nom Nov 04 '14 at 05:47
  • All the comments and answers were correct however, they were not working for me; as I later discovered that my issue was specific to a structure in my code. In the example code above, where I did `f3`, my real code was using `get` on a `map`, which also returns `None`; hence the code was giving unclear errors. Using `map(key)` or `map.getOrElse(...)` solves the problem. – Asad Iqbal Nov 16 '14 at 08:28

2 Answers2

4

In Scala, nested iteration is handled by adding additional <- clauses.

For example, let's say we have two lists l1 and l2 and we want generate over every pair of elements (x,y) where x is in l1 and y is in l2. The syntax in Scala is:

for { x <- l1
      y <- l2
    } yield (x,y)

When no yield keyword follows the for then the entire expression results in a type of Unit, which is the source of your type error. This is useful for performing side effects on the iteration, for example

for { x <- l1
      y <- l2
    } println((x,y))

For more information on for comprehensions see What is Scala's yield?

Community
  • 1
  • 1
lea
  • 408
  • 3
  • 7
  • I understand it. I have tried a couple of times to combine `<-` in one `for` but failed. Could you convert my code ? – Asad Iqbal Nov 02 '14 at 15:24
  • Can you post the code that you tried unsuccessfully? Perhaps there is something else wrong that is being missed in the question. – lea Nov 02 '14 at 17:01
0

Your error may be because you are surrounding yield in {}.

for {stuff} {yield otherstuff}

The form should be:

for {stuff} yield otherstuff

You can of course replace "otherstuff" with a block if you want it to contain multiple expressions so you have:

for {stuff} yield {otherstuff}

Using your example I suspect you want something like:

def useForComprehension(input: Input): List[Output] =
  for {
    o <- splitInputIntoPieces(input)
    restResults <- useForComprehension(subtract(input, o))
    w <- f3(o)
  } yield w :: restResults