Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Last active July 26, 2022 15:49
Embed
What would you like to do?
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