setWhereListenable<K extends Listenable> method

Iterable<K> setWhereListenable<K extends Listenable>(
  1. Iterable<T> others, {
  2. required bool test(
    1. T original,
    2. T other
    ),
  3. required K? apply(
    1. T original,
    2. T other
    ),
  4. K? orElse(
    1. T original
    )?,
})

The data in the list of others is conditionally given to the current list.

If test is true, apply will be executed.

Otherwise, orElse will be executed.

Implementation

Iterable<K> setWhereListenable<K extends Listenable>(
  Iterable<T> others, {
  required bool Function(T original, T other) test,
  required K? Function(T original, T other) apply,
  K? Function(T original)? orElse,
}) {
  final tmp = <K>[];
  for (final original in this) {
    final res = others.firstWhereOrNull((item) => test.call(original, item));
    if (res != null) {
      final applied = apply.call(original, res);
      if (applied != null) {
        tmp.add(applied);
      }
    } else {
      final applied = orElse?.call(original);
      if (applied != null) {
        tmp.add(applied);
      }
    }
  }
  return tmp;
}