recursiveFilterBy method

Iterable<E> recursiveFilterBy(
  1. Iterable<E>? childrenGetter(
    1. E data
    ),
  2. bool prediction(
    1. E data
    )
)

递归筛选符合 prediction 条件的元素。

返回值:包含所有符合条件的元素迭代器。

Implementation

Iterable<E> recursiveFilterBy(
  Iterable<E>? Function(E data) childrenGetter,
  bool Function(E data) prediction,
) {
  final List<E> found = <E>[];

  Iterable<E> find(Iterable<E> data) {
    for (final E e in data) {
      final Iterable<E>? children = childrenGetter(e);
      if (children?.isNotEmpty ?? false) {
        find(children!);
      }

      if (prediction(e)) {
        found.add(e);
      }
    }

    return found;
  }

  return find(this);
}