breakI method

Tuple2<Iterable<T>, Iterable<T>> breakI(
  1. bool predicate(
    1. T t
    )
)

Return a Tuple2 where first element is longest prefix (possibly empty) of this Iterable with elements that do not satisfy predicate and second element is the remainder of the Iterable.

Implementation

Tuple2<Iterable<T>, Iterable<T>> breakI(bool Function(T t) predicate) =>
    foldLeft<Tuple2<bool, Tuple2<Iterable<T>, Iterable<T>>>>(
      const Tuple2(false, 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.mapSecond((second) => second.append(e)))
            : Tuple2(check, a.second.mapFirst((first) => first.append(e)));
      },
    ).second;