, 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
Zach Klippenstein (he/him) [MOD]
04/19/2021, 1:45 PM
map
is inline, so you can return from the outer function inside its lambda.
g
geepawhill
04/19/2021, 1:55 PM
So, something like
.map { x = transform; if(x.isbad) return emptySet(); x }
will work?
z
Zach Klippenstein (he/him) [MOD]
04/19/2021, 2:53 PM
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
geepawhill
04/19/2021, 5:03 PM
I'll try it as soon as I get back to the 'puter. :)