spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

why yield can not work with while loop in scala


Because a while loop is a java equivalent while loop, and the 'for loop' is translated to function call of: <IndexedSeq>.map (if you use yield) or <IndexedSeq>.foreach (if you don't care the result).

Example Scala Code:

class ForVsWhileLoop {
  val dummy = for(i <- 1 to 10) yield i

  var dummy2 = Seq.empty[Int]
  var i = 0
  while(i <= 10)
    dummy2 :+= i
}

Compiles to (scala -Xprint:parse ForVsWhileLoop.scala):

[[syntax trees at end of               
    parser]] // ForVsWhileLoop.scala
package <empty> {
  class ForVsWhileLoop extends scala.AnyRef {
    def <init>() = {
      super.<init>();
      ()
    };

    //
***********************************************
    // the 'for loop' is translated to a function
call
    val dummy = 1.to(10).map(((i) => i));


    var dummy2 = Seq.empty[Int];
    var i = 0;

    // *******************
    // classic while loop
    while$1(){
      if (i.$less$eq(10))
        {
          dummy2.$colon$plus$eq(i);
          while$1()
        }
      else
        ()
    }
  }
}

Categories : Scala

Related to : why yield can not work with while loop in scala
Does Meteor and Iron Router's yield have an event hook? Template.yield.rendered?
If you really need to have the hook on 'any' template, you can define a nested yield template: <template name="layout"> {{> header }} {{> nested_yield }} {{> footer }} </template> <template name="nested_yield"> {{> yield }} </template> then Template.nested_yield.onRendered(function() { //your render code here }); However you might run into i

Categories : Meteor
Yield from coroutine vs yield from task
For the calling co-routine yield from coroutine() feels like a function call (i.e. it will again gain control when coroutine() finishes). yield from Task(coroutine()) on the other hand feels more like creating a new thread. Task() returns almost instantly and chances are there that the caller gains control before coroutine() finishes. The difference between f() and th = threading.Thread(target=f

Categories : Python
Is it possible that a python def contains both "yield" and "return"?
>>> def f(): ... yield 1 ... return 2 ... >>> g = f() >>> next(g) 1 >>> next(g) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration: 2 return in a generator stops its execution and ends the iteration by raising a StopIteration. Apparently, giving a value in return just passes it as an argument to the Stop

Categories : Python
Rails: Dry content_for before yield
You can use helper like this: def yield_something_once unless @yielded_something @yielded_something = true content_for :something end end Or just use content_for(:smth, '') right after yield :smth to clear it.

Categories : Ruby On Rails
Python yield a list with generator
In first you return a generator from itertools import chain def countMoreThanOne(): return (yy for yy in xrange(1,10,2)) print list(countMoreThanOne()) >>> [1, 3, 5, 7, 9] while in this you are yielding a generator so that a generator within the generator def countMoreThanOne(): yield (yy for yy in xrange(1,10,2)) print list(countMoreThanOne()) print list(chain.from_iterab

Categories : Python
Recently Add
Reverse list of n elements
scalac: Error: object CharRef in intelliJ 14
Scala - trouble with type inference in lambda expression
Exception on spark test
How can I emulate Haskell's typeclasses in Scala?
Slick: CRUD extension: How to encapsulate implicit mapping:BaseColumnType[T]
Can't find Traverse for sequencing Seq[ValidationNel[String, MyCaseClass]] => ValidationNel[String, Seq[MyCaseClass]]
Tail recursion: internal "loop" function or default values for accumulators
Scala - Add element:MyType to Array = option[MyType] expected
Scala, Composing Function with two values
Using dependent type to generate compile error
How to match all words in a sentence with scala combinators?
Parser Alternative Operator | Fails
ScalaTest assertion mismatch due to Physical Address
Scala implicit parameter and japanese smiley 'foldLeft'
Is it possible to user reduceByKey((x, y, z) => ...)?
How to implement security Authorization using scala and play?
SSO login using scala script
Sum elements based on a predicate
Keep track of completed Futures
API Observable with dynamic caching
java.io.IOException: Remotely closed in gatling
Scala permutations using two lists
Is Scala Either really a Monad
Spark: Use of distinct
Identifying two type wildcards as identical
how to package spark scala application
Pattern Match on Scala `class`
Type mismatch when using higher-kinded types
Scala List match last element
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.