mergeListsSetsOrQueues function

Iterable mergeListsSetsOrQueues(
  1. Iterable a,
  2. Iterable b
)

Merges two Iterables into one. Supported Iterable types are List, Set, and Queue.

Implementation

Iterable<dynamic> mergeListsSetsOrQueues(
  Iterable<dynamic> a,
  Iterable<dynamic> b,
) {
  final a1 = a.nonNulls;
  final b1 = b.nonNulls;
  Iterable<dynamic> result;
  if (a1 is List) {
    result = List.of(a1);
    var index = 0;
    for (var item in b1) {
      if (result.length <= index || result.elementAt(index) != item) {
        (result as dynamic).add(item);
      }
      index++;
    }
  } else if (a1 is Set) {
    result = Set.of(a1);
    (result as dynamic).addAll(b1);
  } else if (a1 is Queue) {
    result = Queue.of(a1);
    var index = 0;
    for (var item in b1) {
      if (result.length <= index || result.elementAt(index) != item) {
        (result as dynamic).add(item);
      }
      index++;
    }
  } else {
    throw ArgumentError('Unsupported Iterable type');
  }

  return result;
}