You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "Hyukjin Kwon (Jira)" <ji...@apache.org> on 2021/01/10 05:22:00 UTC

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

Hyukjin Kwon created SPARK-34059:
------------------------------------

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


{{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
(v8.3.4#803005)

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