Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Last active July 26, 2022 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongjhih/9ec974286932de1c7d44ad34854b9c47 to your computer and use it in GitHub Desktop.
Save yongjhih/9ec974286932de1c7d44ad34854b9c47 to your computer and use it in GitHub Desktop.
extension PutListX<E> on List<E> {
List<E> putAllBy<K>(Iterable<E> other, K onKey(E item), [void onAdd(E item)?]) {
final Map<K, E> otherMap = other.associateBy<K>((it) => onKey(it));
for (final i in indices) {
final key = onKey(this[i]);
if (otherMap.containsKey(key)) {
this[i] = otherMap.remove(key) as E;
}
}
otherMap.forEach((key, value) {
add(value);
onAdd?.call(value);
});
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment