Persistent collections for Kotlin
The goal of this library is to provide a kotlin API for using persistent (immutable) collections, backed by the fastest known 3rd party implementation.
This library is discontinued. And no support will be provided. I encourage you to use kotlinx.collections.immutable instead.
I created this library, mainly because no other available library was both nice to use from Kotlin and efficient (with state-of-the-art implementations).
But the developement of kotlinx.collections.immutable started again, and:
- they published a version which is no longer backed by
PCollections
- they benchmarked and improved performances of their implementation
- they used state-of-the-art algorithms
- they plan to support Kotlin mutliplatform and they can do it more easily and reliably than I could.
Whith all of that I don't think collekt
has much more value than kotlinx.collections.immutable
. So I prefer invest my time on the developement kwik and other more valuable open source projects (maybe even kotlinx.collections.immutable
).
Collekt doesn't implement the persistent data-structure itself. It is always delegated to an open-source 3rd party.
In order to choose the actual implementation Collekt do performance tests an choose the fastest implementation.
If performance tests show that an new implementation is faster, then the actual implementation will be delegated to the new faster one. That way, as a user of Collekt, you only have to update the version of Collekt to get the fastest state-of-the-art persistent collection. And as the api stay the same, swapping to a faster implementation do not incur any refactoring overhead.
The current implementations are delegated to:
Platform | Library | Author |
---|---|---|
Java (8+) | Paguro | Glen K. Peterson |
JavaScript | Kotlin standard library | JetBrains |
The Persistent interfaces provide +
and -
operator aside other useful methods.
Usage is pretty straight forward if you're used to persistent data structure. Mutation are provided in the form of operator/functions which return a new instance with the operation applied.
val list1 = persistentListOf("Hello", "world")
val list2 = list1 - "world"
val list3 = list2 + "everybody"
val list4 = list3.with(1, "you")
println(list1) // ["Hello", "world"]
println(list2) // ["Hello"]
println(list3) // ["Hello", "everybody"]
println(list4) // ["Hello", "you"]
In most cases it will look exactly the same as if you'd use the standard library.
But unlike the +
and -
operator of the standard library, the collections are not copied, and most of the data is shared making them much faster and less memory consuming.
If you want to test the project in its current state, you can get the artifacts for maven or gradle from Jitpack:
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
dependencies {
// For Java (JDK 8+)
compile 'com.github.jcornaz.collekt:collekt-core-jvm:0.0.3'
// For javascript (implementations are not efficient yet)
compile 'com.github.jcornaz.collekt:collekt-core-js:0.0.3'
// For common module
compile 'com.github.jcornaz.collekt:collekt-core-common:0.0.3'
}
The current known challengers for a JVM implementation are:
- Paguro (currently used for all collection types)
- vavr.io
- kotlinx.collections.immutable
The following libraries are not eligible because they are discontinued and/or don't support null values:
For Javascript the only known challenger is:
If you know another fast self-contained jvm or javascript implementation, please submit an issue.