Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

loops - Scala "<-" for comprehension

I have found that Scala always has a "natural explanation" to anything. Always something like "ohh, but that's just a function being called on this and that object with this and that parameter". In a sense, nothing is really compiler-magic as we know it from other languages.

My question is on the <- operator as used in the following code:

for(i <- 0 to 10) println(i)

In this example I can see it being rewritten to something like:

0.to(10).foreach((i:Int)=>println(i))

but this doesn't explain how the i got carried into the anonymous function inside the foreach function. At the point where you write i it is not an object, and not yet a declared variable. So what is it, and how is it being carried over to the inside of foreach?

My guess is that I finally discovered something which is in fact compiler magic

Thanks for your time.

To clarify, my question is: how does the <- operator work in the 1st line of code since i is not an object on which it can be called as a function.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To augment Dave's answer, here is a translation schema for 'for-comprehensions' from Scala language specification:

A comprehension for (enums) yield e evaluates expression e for each binding generated by the enumerators enums. An enumerator sequence always starts with a generator; this can be followed by further generators, value definitions, or guards.

A generator p <- e produces bindings from an expression e which is matched in some way against pattern p. A value definition val p = e binds the value name p (or several names in a pattern p) to the result of evaluating the expression e. A guard if e contains a boolean expression which restricts enumerated bindings.

The precise meaning of generators and guards is defined by translation to invocations of four methods: map, filter, flatMap, and foreach. These methods can be implemented in different ways for different carrier types.

The translation scheme is as follows. In a first step, every generator p <- e, where p is not irrefutable (§8.1) for the type of e is replaced by

 p <- e.filter { case p => true; case _ => false }

Then, the following rules are applied repeatedly until all comprehensions have been eliminated.

  • A for-comprehension for (p <- e) yield e0 is translated to e.map { case p => e0 }.

  • A for-comprehension for (p <- e) e0 is translated to e.foreach { case p => e0 }.

  • A for-comprehension for (p <- e; p0 <- e0 . . .) yield e00, where . . . is a (possibly empty) sequence of generators or guards, is translated to:
    e.flatMap { case p => for (p0 <- e0 . . .) yield e00 }.

  • A for-comprehension for (p <- e; p0 <- e0 . . .) e00 where . . . is a (possibly empty) sequence of generators or guards, is translated to:
    e.foreach { case p => for (p0 <- e0 . . .) e00 } .

  • A generator p <- e followed by a guard if g is translated to a single generator:
    p <- e.filter((x1, . . . , xn) => g )
    where x1, . . . , xn are the free variables of p.

  • A generator p <- e followed by a value definition val p0 = e0 is translated to the following generator of pairs of values, where x and x0 are fresh names:

    val (p, p0) <- 
      for(x@p <- e) yield { val x0@p0 = e0; (x, x0) }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...