Something like this? (I haven't tested it) ``` fun...
# announcements
r
Something like this? (I haven't tested it)
Copy code
fun <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.)
💯 1
a
I think
predicate(it, item)
is a bug.
Or did you intend to call it with the same parameter twice? In that line,
it
and
item
are equivalent aren't they?
r
No,
it
is coming from
indexOfFirst
which is called on `this, and
item
is coming from
source
a
Ohhh gotcha
derp sorry
👍 1
p
I will test this when I get home