You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "Sean Owen (JIRA)" <ji...@apache.org> on 2016/07/24 07:28:20 UTC

[jira] [Created] (SPARK-16694) Use for/foreach rather than map for Unit expressions whose side effects are required

Sean Owen created SPARK-16694:
---------------------------------

             Summary: Use for/foreach rather than map for Unit expressions whose side effects are required
                 Key: SPARK-16694
                 URL: https://issues.apache.org/jira/browse/SPARK-16694
             Project: Spark
          Issue Type: Improvement
          Components: Examples, MLlib, Spark Core, SQL, Streaming
            Reporter: Sean Owen
            Assignee: Sean Owen
            Priority: Minor


{{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.

{code}
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

{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org