Ruckus
08/01/2018, 7:13 PMfun <T> MutableList<T>.upsert(source: List<T>, predicate: (existing: T, new: T) -> Boolean) {
for (item in source) {
val index = indexOfFirst { predicate(it, item) }
if (index >= 0) set(index, item) else add(item)
}
}
(Note that it's adding to the list while still using it. You may want to save new items to a separate list and add them all at the end after processing.)adam-mcneilly
08/01/2018, 7:15 PMpredicate(it, item)
is a bug.adam-mcneilly
08/01/2018, 7:15 PMit
and item
are equivalent aren't they?Ruckus
08/01/2018, 7:17 PMit
is coming from indexOfFirst
which is called on `this, and item
is coming from source
adam-mcneilly
08/01/2018, 7:17 PMadam-mcneilly
08/01/2018, 7:17 PMpaulex
08/01/2018, 7:26 PM