kotlinx.collections.immutable icon indicating copy to clipboard operation
kotlinx.collections.immutable copied to clipboard

Consider adding filterTo, mapTo, etc overloads

Open belyaev-mikhail opened this issue 3 years ago • 3 comments
trafficstars

It is very inconvenient with current API to use map(), filter() and other collection combinators, which should be the natural default style to use when working with immutable collections.

persistentList.map { it + 1 }.toPersistentList()

is extremely inefficient and

persistentList.mapTo(persistentListOf().builder()) { it + 1 }.build()

is just plain ugly.

I propose to introduce overloads for mapTo, filterTo, etc. combinators to the API. In fact, I end up defining these myself in every project I try to use ks.collections.immutable in.

Another possibility is to introduce map, filter, etc. directly, but it would suffer from the same problems that operators + and - currently have.

belyaev-mikhail avatar Jan 13 '22 16:01 belyaev-mikhail

what about working with List, and when u are sure that u will get into an Immutable state using the toImmutableList? honest question, not sure why an Immutable should have transforms on it? If you wanna make another Immutable you will still be creating a new object anyways.

desgraci avatar Jan 04 '23 14:01 desgraci

persistentList.map { it + 1 }.toPersistentList() creates two intermediate objects instead of one. Also, in the particular case of filter, some persistent data structures can implement it in a persistent-friendly fashion, while filter {}.toPersistentList() obviously can't do it.

belyaev-mikhail avatar Jan 04 '23 14:01 belyaev-mikhail

Ah ok, got, thanks for the quick explanation and response, I knew I was missing something basic :D this is Immutable to Immutable, which makes sense for creating new ones.

desgraci avatar Jan 04 '23 14:01 desgraci