Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-16694

Use for/foreach rather than map for Unit expressions whose side effects are required

    XMLWordPrintableJSON

Details

    Description

      map is misused in many places where foreach is intended. This caused a bug in https://issues.apache.org/jira/browse/SPARK-16664 and might be a latent bug elsewhere; it's also easy to find with IJ inspections. Worth patching up.

      To illustrate the general problem, map happens to work in Scala where the collection isn't lazy, but will fail to execute the code when it is. map also causes a collection of Unit to be created pointlessly.

      scala> val foo = Seq(1,2,3)
      foo: Seq[Int] = List(1, 2, 3)
      
      scala> foo.map(println)
      1
      2
      3
      res0: Seq[Unit] = List((), (), ())
      
      scala> foo.view.map(println)
      res1: scala.collection.SeqView[Unit,Seq[_]] = SeqViewM(...)
      
      scala> foo.view.foreach(println)
      1
      2
      3
      
      

      Attachments

        Issue Links

          Activity

            People

              srowen Sean R. Owen
              srowen Sean R. Owen
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: