I have `method(input:Set<X>):Set<X>`, ...
# announcements
g
I have
method(input:Set<X>):Set<X>
, which wants to transform each element of the (small) set, seemingly a natural fit with
.map { }
, but, the transformation can produce invalid individual values, and if that occurs the method's result will be
emptySet()
. In other words, under some conditions i want to short-circuit the mapping and just return the empty set. I am wondering if there is some clever stream-ish/sequence-ish way to accomplish this, or should I just go with a for-in and a result accumulator?
z
map
is inline, so you can return from the outer function inside its lambda.
g
So, something like
.map { x = transform; if(x.isbad) return emptySet(); x }
will work?
z
The easiest way to find out is to try it 😉 But yes, as long as your collection is not a
Sequence
(sequence operators are not inline).
g
I'll try it as soon as I get back to the 'puter. :)