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

Fixed toPersistentSet, toPersistentMap to work with any impl. #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions core/commonMain/src/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,14 @@ fun CharSequence.toImmutableSet(): PersistentSet<Char> = toPersistentSet()
* If the receiver is already a persistent set, returns it as is.
* If the receiver is a persistent set builder, calls `build` on it and returns the result.
*
* Elements of the returned set are iterated in the same order as in this collection.
* * If the receiver is not a persistent set builder,
* elements of the returned set are iterated in the same order as in this collection.
* Otherwise the order depends on the persistent set builder implementation.

*/
fun <T> Iterable<T>.toPersistentSet(): PersistentSet<T> =
this as? PersistentOrderedSet<T>
?: (this as? PersistentOrderedSetBuilder)?.build()
this as? PersistentSet<T>
?: (this as? PersistentSet.Builder)?.build()
?: PersistentOrderedSet.emptyOf<T>() + this

/**
Expand Down Expand Up @@ -715,11 +718,13 @@ fun <K, V> Map<K, V>.toImmutableMap(): ImmutableMap<K, V>
* If the receiver is already a persistent map, returns it as is.
* If the receiver is a persistent map builder, calls `build` on it and returns the result.
*
* Entries of the returned map are iterated in the same order as in this map.
* If the receiver is not a persistent map builder,
* entries of the returned map are iterated in the same order as in this map.
* Otherwise the order depends on the persistent map builder implementation.
*/
fun <K, V> Map<K, V>.toPersistentMap(): PersistentMap<K, V>
= this as? PersistentOrderedMap<K, V>
?: (this as? PersistentOrderedMapBuilder<K, V>)?.build()
= this as? PersistentMap<K, V>
?: (this as? PersistentMap.Builder<K, V>)?.build()
?: PersistentOrderedMap.emptyOf<K, V>().putAll(this)

/**
Expand Down