score:4
Accepted answer
Consider the following slightly simpler example:
def foo[A, B](e: Either[A, B]) = e.fold(
{ println("Launch the missiles!"); Left(_) },
Right(_)
)
Missiles are going to get launched every time you call foo
, because the first argument we're passing isn't exactly a function: it's a block that evaluates to a function, and that block's going to get evaluated whether or not the function is used.
See this blog post by Jesse Eichar for a more detailed discussion of this behavior.
Source: stackoverflow.com
Related Query
- Strange fold scoping issue
- Strange issue with SBT, println, and scala console application
- Strange behavior of fold
- Scoping Issue with SparkContext.sequenceFile(...).foreach in Scala
- strange performance issue with scala
- Strange issue with using Java ProcessBuilder on Windows 2008 R2 Standard
- Strange issue regarding for-comprehension
- Strange behavior with reduce and fold on identical code block
- Reduce, fold or scan (Left/Right)?
- Abort early in a fold
- Difference between fold and foldLeft or foldRight?
- Fold and foldLeft method difference
- Functional programming, Scala map and fold left
- Scala : fold vs foldLeft
- Strange sbt bug where I cannot import sbt project due to keys colliding with themselves
- Scala foreach strange behaviour
- Private scoping with square brackets (private[...]) in Scala
- Why doesn't Option have a fold method?
- In Scala, how do I fold a List and return the intermediate results?
- Scala case having 22 fields but having issue with play-json in scala 2.11.5
- Difference between reduce and fold
- IntelliJ Scala configuration issue
- ClassNotFoundException:scala.PreDef$ issue
- Scala Vector fold syntax (/: and :\ and /:\)
- strange error message: bad symbolic reference. A signature in package.class refers to term apache in package org which is not available
- Scala: Producing the intermediate results of a fold
- The strange case of multiple Futures in Scala
- Scala fold right and fold left
- Spark ML VectorAssembler returns strange output
- Playframework 2.3.0 issue with Scala IDE (Kepler)
More Query from same tag
- How to handle an unknown number of futures that depend on the previous result?
- Missing import scala.collection.parallel in Scala 2.13
- How to make a nested toSet in scala in an idiomatic way?
- Dependent method types and type-classes
- Functor implementation for types with more than one type
- Does another Git Repo (Bitbucket on premise) other than Github not work for 'sbt new OtherGitRepo'
- Test REST service using Scala Lift
- how can i create a custom CellEncoder in kantan
- What is a elegant way of writing case statement in Scala Spark dataframe?``
- How can I send multiple http requests asynchronous with Netty?
- error: not found: value isEven and error: not found: value isOdd
- Apache Spark distance between two points using squaredDistance
- Scala Form email type mismatched in my models
- Getting the elements in a 2D vector (matrix)
- Circe decode case class
- How do I set schema for map field in big query?
- How does def d = (x: int) => f(x) get used?
- permanently hidden warning in scala application
- Simple 3D graphics in Scala
- Dynamic (down)casting in Scala
- How to convert Map to Json using Json4s
- What is the advantage of using Option.map over Option.isEmpty and Option.get?
- how to inject dependencies to a service with MacWire (play framework)
- Scala - Computing strongly connected components - stack overflow error
- Should 'require' go inside or outside of the Future?
- Is there a runWithValidation feature for Gradient Boosted Trees (GBT) in Spark ml?
- Doobie and DB access composition within 1 transaction
- Stream stdout from scala Process
- Does the download of Scala IDE 4.0.0 for Eclipse include SBT?
- Type parameters, upper bounds and overriding