When building up a collection inside an Option
, each attempt to make the next member of the collection might fail, making the collection as a whole a failure, too. Upon the first failure to make a member, I'd like to give up immediately and return None
for the whole collection. What is an idiomatic way to do this in Scala?
Here's one approach I've come up with:
def findPartByName(name: String): Option[Part] = . . .
def allParts(names: Seq[String]): Option[Seq[Part]] =
names.foldLeft(Some(Seq.empty): Option[Seq[Part]]) {
(result, name) => result match {
case Some(parts) =>
findPartByName(name) flatMap { part => Some(parts :+ part) }
case None => None
}
}
In other words, if any call to findPartByName
returns None
, allParts
returns None
. Otherwise, allParts
returns a Some
containing a collection of Parts
, all of which are guaranteed to be valid. An empty collection is OK.
The above has the advantage that it stops calling findPartByName
after the first failure. But the foldLeft
still iterates once for each name, regardless.
Here's a version that bails out as soon as findPartByName
returns a None
:
def allParts2(names: Seq[String]): Option[Seq[Part]] = Some(
for (name <- names) yield findPartByName(name) match {
case Some(part) => part
case None => return None
}
)
I currently find the second version more readable, but (a) what seems most readable is likely to change as I get more experience with Scala, (b) I get the impression that early return
is frowned upon in Scala, and (c) neither one seems to make what's going on especially obvious to me.
The combination of "all-or-nothing" and "give up on the first failure" seems like such a basic programming concept, I figure there must be a common Scala or functional idiom to express it.
Scala collections have some options to use laziness to achieve that.
You can use
view
andtakeWhile
:Using
ifDefinedAt
avoids potentially traversing a long inputnames
in the case of an early failure.You could also use
toStream
andspan
to achieve the same thing:I've found trying to mix
view
andspan
causesfindPartByName
to be evaluated twice per item in case of success.The whole idea of returning an error condition if any error occurs does, however, sound more like a job ("the" job?) for throwing and catching exceptions. I suppose it depends on the context in your program.
Combining the other answers, i.e., a mutable flag with the map and takeWhile we love.
Given an infinite stream:
Take until a predicate fails:
or more simply:
The
return
in your code is actually a couple levels deep in anonymous functions. As a result, it must be implemented by throwing an exception which is caught in the outer function. This isn't efficient or pretty, hence the frowning.It is easiest and most efficient to write this with a
while
loop and anIterator
.Because we don't know what kind of
Seq
names
is, we must create an iterator to loop over it efficiently rather than usingtail
or indexes. The while loop can be replaced with a tail-recursive inner function, but with the iterator awhile
loop is clearer.I keep thinking that this has to be a one- or two-liner. I came up with one:
Advantage:
Disadvantages:
The early
return
violates referential transparency, as Aldo Stracquadanio pointed out. You can't put the body ofallParts4
into its calling code without changing its meaning.Possibly inefficient due to the internal throwing and catching of an exception, as wingedsubmariner pointed out.
Sure enough, I put this into some real code, and within ten minutes, I'd enclosed the expression inside something else, and predictably got surprising behavior. So now I understand a little better why early
return
is frowned upon.This is such a common operation, so important in code that makes heavy use of
Option
, and Scala is normally so good at combining things, I can't believe there isn't a pretty natural idiom to do it correctly.Aren't monads good for specifying how to combine actions? Is there a
GiveUpAtTheFirstSignOfResistance
monad?I think your
allParts2
function has a problem as one of the two branches of your match statement will perform a side effect. Thereturn
statement is the not-idiomatic bit, behaving as if you are doing an imperative jump.The first function looks better, but if you are concerned with the sub-optimal iteration that foldLeft could produce you should probably go for a recursive solution as the following:
I didn't compile/run it but the idea should be there and I believe it is more idiomatic than using the return trick!