span method
- bool predicate(
- T t
Return a Tuple2 where first element is longest prefix (possibly empty) of this Iterable
with elements that satisfy predicate
and second element is the remainder of the Iterable
.
Implementation
Tuple2<Iterable<T>, Iterable<T>> span(bool Function(T t) predicate) =>
foldLeft<Tuple2<bool, Tuple2<Iterable<T>, Iterable<T>>>>(
const Tuple2(true, Tuple2([], [])),
(a, e) {
if (!a.first) {
return Tuple2(
a.first, a.second.mapSecond((second) => second.append(e)));
}
final check = predicate(e);
return check
? Tuple2(check, a.second.mapFirst((first) => first.append(e)))
: Tuple2(check, a.second.mapSecond((second) => second.append(e)));
},
).second;