17

Here is the standard format for a for/yield in scala: notice it expects a collection - whose elements drive the iteration.

for (blah <- blahs) yield someThingDependentOnBlah

I have a situation where an indeterminate number of iterations will occur in a loop. The inner loop logic determines how many will be executed.

while (condition) { some logic that affects the triggering condition } yield blah

Each iteration will generate one element of a sequence - just like a yield is programmed to do. What is a recommended way to do this?

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • This is kind of vague. `for ... yield` maps an immutable structure to another immutable structure, but `while` is generally used for manipulating some mutable state, so there isn't an obvious `yield` analog. Could you provide more details about what you're actually trying to do? – Michael Zajac Oct 25 '14 at 00:44

3 Answers3

28

You can

Iterator.continually{ some logic; blah }.takeWhile(condition)

to get pretty much the same thing. You'll need to use something mutable (e.g. a var) for the logic to impact the condition. Otherwise you can

Iterator.iterate((blah, whatever)){ case (_,w) => (blah, some logic on w) }.
         takeWhile(condition on _._2).
         map(_._1)
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
2

Using for comprehensions is the wrong thing for that. What you describe is generally done by unfold, though that method is not present in Scala's standard library. You can find it in Scalaz, though.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • OK. i just did a normal while loop and inside of it appended to an ArrayBuffer – WestCoastProjects Oct 25 '14 at 01:12
  • 1
    Scala standard collections have huge functionality holes in them.. but switching to scalaz is a little radical for the rest of us... – matanster Mar 08 '16 at 19:40
  • 1
    @matanster That's like saying using Apache Commons is a little radical. Using _all_ Apache Commons might be radical, but, say, just commons-lang is fine. It just so happens that Scalaz comes with everything in it, but you *can* use it just for, to use the example, `unfold`. – Daniel C. Sobral Mar 10 '16 at 00:13
  • Oh well, you have a point there. Alas the learning curve.. but is the performance of scalaz at par? – matanster Mar 10 '16 at 18:13
  • Then I might try it, but this means an even longer learning curve for anyone who reads my Scala code... hope you are right :) – matanster Mar 16 '16 at 10:29
  • I typically work in teams that have newbies to scala (data science teams with python/R or ETL teams with java/SQL). `scalaz` enhances scala to the point of being essentially a different dialect: so adding that to the mix is unlikely to "fly". – WestCoastProjects Sep 16 '17 at 21:19
1

Another way similar to suggestion by @rexkerr:

 blahs.toIterator.map{ do something }.takeWhile(condition)

This feels a bit more natural than the Iterator.continually

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560