kopykat-kt / kopykat

Little utilities for more pleasant immutable data in Kotlin
Other
283 stars 16 forks source link

Mutable utilities #49

Closed serras closed 2 years ago

serras commented 2 years ago

These utilities should be useful in combination with the mutable copy. For example, if you want to map over a list, the developer would be forced to use toMutableList at the end to please the compiler. But now the can do:

person.copy {
  jobs.mapInPlace { it.capitalize() }
}
serras commented 2 years ago

This is really good. Just have a few thoughts:

1. Instead of using mapInPlace we could use something like `mutateAll` or `changeAll` (for all versions). It wasn't obvious (for me) ar first that the in place meant that it was mutating the collection.

Naming is always hard. My mind goes quickly into map for changing all elements; maybe we can keep mapInPlace and mutateAll?

2. Should we include more options for maps? Like `mapKeys` and `map`.

3. Should we cover other collections like set, sequences, etc (had to ask before someone else asks 🤭)

The problem with (2) and (3) is that there's no easy way to implement those while really reusing the same structure. I tried a bit with mapKeys, and every solution that I found wasn't better than simply:

map = map.mapKeys { ... }.toMutableMap()
pablisco commented 2 years ago

Map, flatmap (oh maybe could have that too), etc are copy operations. So in terms of expectations, I would expect it to return a new copy when I do a map. That's why I suggested the use of mutateAll, or maybe mutateEach is a better one. (And maybe flatMutateEach?)

Oh, I forgot, we may want to also have other transformations like filter, mapNotNull, etc.

If you want we can merge this (maybe with the name changes) and I'll do a separate one. O wanna give this a try, if you don't mind hehe.