Last active
July 26, 2022 15:49
-
-
Save yongjhih/9ec974286932de1c7d44ad34854b9c47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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