Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value assignment inside for-loop in Scala

Tags:

for-loop

scala

Is there any difference between this code:

    for(term <- term_array) {
        val list = hashmap.get(term)
        ...
    }

and:

    for(term <- term_array; val list = hashmap.get(term)) {
        ...
    }

Inside the loop I'm changing the hashmap with something like this

hashmap.put(term, string :: list)

While checking for the head of list it seems to be outdated somehow when using the second code snippet.

like image 344
Felipe Hummel Avatar asked Jun 22 '11 23:06

Felipe Hummel


People also ask

What is Val in a for loop?

Syntax of for Loop Here, val is the variable that takes the value of the item inside the sequence on each iteration.

How do you make a nested for loop in Scala?

For nesting loops, we can place the second loop inside the body of the first loop. This is the traditional way and most used one too. And is done like this, loop1{ loop2{ //code to be executed… } }

How many ways we can use for loop in Scala?

We can also use multiple ranges in single for-loop. These ranges are separated by a semi-colon(;). Let us discuss with the help of an example. In the below example, we use two different ranges into a single loop, i.e, w <- 0 to 3; z<- 8 until 10.


1 Answers

The difference between the two is, that the first one is a definition which is created by pattern matching and the second one is a value inside a function literal. See Programming in Scala, Section 23.1 For Expressions:

  for {
    p <- persons              // a generator
    n = p.name                // a definition
    if (n startsWith "To")    // a filter
  } yield n

You see the real difference when you compile sources with scalac -Xprint:typer <filename>.scala:

object X {
  val x1 = for (i <- (1 to 5); x = i*2) yield x
  val x2 = for (i <- (1 to 5)) yield { val x = i*2; x }
}

After code transforming by the compiler you will get something like this:

private[this] val x1: scala.collection.immutable.IndexedSeq[Int] =
  scala.this.Predef.intWrapper(1).to(5).map[(Int, Int), scala.collection.immutable.IndexedSeq[(Int, Int)]](((i: Int) => {
    val x: Int = i.*(2);
    scala.Tuple2.apply[Int, Int](i, x)
  }))(immutable.this.IndexedSeq.canBuildFrom[(Int, Int)]).map[Int, scala.collection.immutable.IndexedSeq[Int]]((
    (x$1: (Int, Int)) => (x$1: (Int, Int) @unchecked) match {
      case (_1: Int, _2: Int)(Int, Int)((i @ _), (x @ _)) => x
    }))(immutable.this.IndexedSeq.canBuildFrom[Int]);

private[this] val x2: scala.collection.immutable.IndexedSeq[Int] =
  scala.this.Predef.intWrapper(1).to(5).map[Int, scala.collection.immutable.IndexedSeq[Int]](((i: Int) => {
    val x: Int = i.*(2);
    x
  }))(immutable.this.IndexedSeq.canBuildFrom[Int]);

This can be simplified to:

val x1 = (1 to 5).map {i =>
    val x: Int = i * 2
    (i, x)
  }.map {
    case (i, x) => x
  }

val x2 = (1 to 5).map {i =>
    val x = i * 2
    x
  }
like image 196
kiritsuku Avatar answered Sep 28 '22 22:09

kiritsuku