Value assignment inside for-loop in Scala

后端 未结 2 1471
北荒
北荒 2021-02-20 01:36

Is there any difference between this code:

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

and:

相关标签:
2条回答
  • Instantiating variables inside for loops makes sense if you want to use that variable the for statement, like:

    for (i <- is; a = something; if (a)) {
       ...
    }
    

    And the reason why your list is outdated, is that this translates to a foreach call, such as:

    term_array.foreach {
       term => val list= hashmap.get(term)
    } foreach {
      ...
    }
    

    So when you reach ..., your hashmap has already been changed. The other example translates to:

    term_array.foreach {
       term => val list= hashmap.get(term)
       ...
    }
    
    0 讨论(0)
  • 2021-02-20 02:26

    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
      }
    
    0 讨论(0)
提交回复
热议问题