Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate with KotlinX Immutable Collections, add Flow.toMap #4273

Open
Laxystem opened this issue Nov 14, 2024 · 4 comments
Open

Integrate with KotlinX Immutable Collections, add Flow.toMap #4273

Laxystem opened this issue Nov 14, 2024 · 4 comments

Comments

@Laxystem
Copy link

Laxystem commented Nov 14, 2024

This could be done fairly easily:

suspend fun <T> Flow<T>.toPersistentList() = emptyPersistentHashList<T>().mutate { toList(it) }
public suspend fun <K, V, M : MutableMap<in K, in V>> Flow<Pair<K, V>>.toMap(destination: M): M {
    collect { (key, value) ->
        destination[key] = value
    }
    return destination
}

suspend fun <K, V> Flow<Pair<K, V>>.toPersistentHashMap() = emptyPersistentHashMap<Pair<K, V>>().mutate { toMap(it) }

I used mutate instead of toXxx(mutableXxxOf()).toPersistentXxx() as the mutate function uses builders, that KotlinX Immutable Collections can optimize.

A nice addition to complement the toMap that is necessary for this suggestion would be a Map.asFlow.

@dkhalanskyjb
Copy link
Collaborator

dkhalanskyjb commented Nov 14, 2024

toPersistentList is something that we can add according to the decision in #4212 (comment). toPersistentHashMap is questionable, because we don't even have that for List or Sequence, and Flow is the next step.

cc @qurbonzoda

@Laxystem
Copy link
Author

toPersistentHashMap is questionable, because we don't even have that for List or Sequence, and Flow is the next step.

I'd argue that we should have it for them as well, and that there's no reason not to add them to Flow in the meanwhile.

@qurbonzoda
Copy link
Contributor

I am wondering if this integration requires publishing a separate artifact (perhaps in kx-collections-immutable) with relevant functions?

@Laxystem
Copy link
Author

@qurbonzoda it does, unless you want to introduce a dependency on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants