Value assignment inside for-loop in Scala

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

Is there any difference between this code:

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

and:

2条回答
  •  不知归路
    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 .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
      }
    

提交回复
热议问题