Scala for Expression

From GM-RKB
Jump to navigation Jump to search

A Scala for Expression is a foreach Loop Expression that is a Scala Loop Expression.



References

2014

  • (Wikipedia, 2014) ⇒ http://en.wikipedia.org/wiki/Scala_(programming_language)#For-expressions Retrieved:2014-1-13.
    • Instead of the Java "foreach" loops for looping through an iterator, Scala has a much more powerful concept of for-expressions. These are similar to list comprehensions in a languages such as Haskell, or a combination of list comprehensions and generator expressions in Python. For-expressions using the yield keyword allow a new collection to be generated by iterating over an existing one, returning a new collection of the same type. They are translated by the compiler into a series of map, flatMap and filter calls. Where yield is not used, the code approximates to an imperative-style loop, by translating to foreach.

      A simple example is:

      <source lang=Scala>

      val s = for (x <- 1 to 25 if x*x > 50) yield 2*x

      </source>

      The result of running it is the following vector:

       :Vector(16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50)

      (Note that the expression 1 to 25 is not special syntax. The method to is rather defined in the standard Scala library as an extension method on integers, using a technique known as implicit conversions[1] that allows new methods to be added to existing types.)

      A more complex example of iterating over a map is:

      <source lang=Scala>

      // Given a map specifying Twitter users mentioned in a set of tweets,

      // and number of times each user was mentioned, look up the users

      // in a map of known politicians, and return a new map giving only the

      // Democratic politicians (as objects, rather than strings).

      val dem_mentions = for {

      (mention, times) <- mentions

      account <- accounts.get(mention)

      if account.party == "Democratic"

      } yield (account, times)

      </source>

      Note that the expression (mention, times) <- mentions is actually an example of pattern matching (see below). Iterating over a map returns a set of key-value tuples, and pattern-matching allows the tuples to easily be destructured into separate variables for the key and value. Similarly, the result of the comprehension also returns key-value tuples, which are automatically built back up into a map because the source object (from the variable mentions) is a map. Note that if mentions instead held a list, set, array or other collection of tuples, exactly the same code above would yield a new collection of the same type.

  1. "Pimp my Library". Artima.com. 2006-10-09. http://www.artima.com/weblogs/viewpost.jsp?thread=179766. Retrieved 2013-06-25. 

2012

  • http://www.tutorialspoint.com/scala/scala_for_loop.htm
    • QUOTE: A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. There are various forms of for loop in Scala which are described below:
      The for Loop with Ranges The simplest syntax of a for loop in Scala is:
      for( var x <- Range ){ statement(s);}

      Here, the Range could be a range of numbers and that is represented as i to j or sometime like i until j. The left-arrow <- operator is called a generator, so named because it's generating individual values from a range. Example: Following is the example of for loop with range using i to j syntax:

 object Test {
  def main(args: Array[String]) {
     var a = 0;
     // for loop execution with a range
     for( a <- 1 to 10){
        println( "Value of a: " + a );
     }
  }
}