@@ -9,40 +9,77 @@ For further details see the [proposal](proposal.md).
9
9
## What's in this library
10
10
### Interfaces and implementations
11
11
12
- This library provides interfaces for immutable persistent collections:
13
-
14
- | Interface | Bases | Implementations |
15
- | ----------| ----- | --------------- |
16
- | ` ImmutableCollection ` | ` Collection `
17
- | ` ImmutableList ` | ` ImmutableCollection ` , ` List ` | ` immutableListOf ` |
18
- | ` ImmutableSet ` | ` ImmutableCollection ` , ` Set ` | ` immutableSetOf ` , ` immutableHashSetOf ` |
19
- | ` ImmutableMap ` | ` Map ` | ` immutableMapOf ` , ` immutableHashMapOf ` |
20
-
21
- The default implementations of ` ImmutableSet ` and ` ImmutableMap ` , which are returned by ` immutableSetOf ` and ` immutableMapOf `
12
+ This library provides interfaces for immutable and persistent collections.
13
+
14
+ #### Immutable collection interfaces
15
+
16
+ | Interface | Bases
17
+ | ----------| -----
18
+ | ` ImmutableCollection ` | ` Collection ` |
19
+ | ` ImmutableList ` | ` ImmutableCollection ` , ` List ` |
20
+ | ` ImmutableSet ` | ` ImmutableCollection ` , ` Set ` |
21
+ | ` ImmutableMap ` | ` Map ` |
22
+
23
+ #### Persistent collection interfaces
24
+
25
+ | Interface | Bases
26
+ | ----------| -----
27
+ | ` PersistentCollection ` | ` ImmutableCollection ` |
28
+ | ` PersistentList ` | ` PersistentCollection ` , ` ImmutableList ` |
29
+ | ` PersistentSet ` | ` PersistentCollection ` , ` ImmutableSet ` |
30
+ | ` PersistentMap ` | ` ImmutableMap ` |
31
+
32
+ #### Persistent collection builder interfaces
33
+
34
+ | Interface | Bases
35
+ | ----------| -----
36
+ | ` PersistentCollection.Builder ` | ` MutableCollection ` |
37
+ | ` PersistentList.Builder ` | ` PersistentCollection.Builder ` , ` MutableList ` |
38
+ | ` PersistentSet.Builder ` | ` PersistentCollection.Builder ` , ` MutableSet ` |
39
+ | ` PersistentMap.Builder ` | ` MutableMap ` |
40
+
41
+
42
+ To instantiate an empty persistent collection or a collection with the specified elements the functions
43
+ ` persistentListOf ` , ` persistentSetOf ` , and ` persistentMapOf ` can be used.
44
+
45
+ The default implementations of ` PersistentSet ` and ` PersistentMap ` , which are returned by ` persistentSetOf ` and ` persistentMapOf ` ,
22
46
preserve the element insertion order during iteration. This comes at expense of maintaining more complex data structures.
23
- If the order of elements doesn't matter, more efficient ` immutableHashSetOf ` and ` immutableHashMapOf ` could be used.
47
+ If the order of elements doesn't matter, the more efficient implementations returned by the functions
48
+ ` persistentHashSetOf ` and ` persistentHashMapOf ` could be used.
24
49
25
50
### Operations
26
51
27
52
#### toImmutableList/Set/Map
53
+
28
54
Converts a read-only or mutable collection to an immutable one.
29
- If the receiver is already immutable and has the required type, returns itself .
55
+ If the receiver is already immutable and has the required type, returns it as is .
30
56
31
57
``` kotlin
32
58
fun Iterable<T>.toImmutableList (): ImmutableList <T >
33
59
fun Iterable<T>.toImmutableSet (): ImmutableSet <T >
34
60
```
35
61
62
+ #### toPersistentList/Set/Map
63
+
64
+ Converts a read-only or mutable collection to a persistent one.
65
+ If the receiver is already persistent and has the required type, returns it as is.
66
+ If the receiver is a builder of the required persistent collection type, calls ` build ` on it and returns the result.
67
+
68
+ ``` kotlin
69
+ fun Iterable<T>.toPersistentList (): PersistentList <T >
70
+ fun Iterable<T>.toPersistentSet (): PersistentSet <T >
71
+ ```
72
+
36
73
#### ` + ` and ` - ` operators
37
74
38
- ` plus ` and ` minus ` operators on immutable collections exploit their immutability
75
+ ` plus ` and ` minus ` operators on persistent collections exploit their immutability
39
76
and delegate the implementation to the collections themselves.
40
77
The operation is performed with persistence in mind: the returned immutable collection may share storage
41
78
with the original collection.
42
79
43
80
``` kotlin
44
- val newList = immutableListOf (" a" , " b" ) + " c"
45
- // newList is also ImmutableList
81
+ val newList = persistentListOf (" a" , " b" ) + " c"
82
+ // newList is also a PersistentList
46
83
```
47
84
48
85
> ** Note:** you need to import these operators from ` kotlinx.collections.immutable ` package
@@ -55,8 +92,8 @@ import kotlinx.collections.immutable.*
55
92
56
93
#### Mutate
57
94
58
- ` mutate ` extension function simplifies quite common pattern of immutable collection modification:
59
- get a builder, apply some mutating operations on it, transform it back to an immutable collection:
95
+ ` mutate ` extension function simplifies quite common pattern of persistent collection modification:
96
+ get a builder, apply some mutating operations on it, transform it back to a persistent collection:
60
97
61
98
``` kotlin
62
99
collection.builder().apply { some_actions_on(this ) }.build()
@@ -70,12 +107,11 @@ collection.mutate { some_actions_on(it) }
70
107
71
108
## Using in your projects
72
109
73
- > Note that these libraries are experimental and are subject to change.
110
+ > Note that the library is experimental and the API is subject to change.
74
111
75
- The libraries are published to [ kotlinx] ( https://bintray.com/kotlin/kotlinx/kotlinx.collections.immutable ) bintray repository.
112
+ The library is published to [ kotlinx] ( https://bintray.com/kotlin/kotlinx/kotlinx.collections.immutable ) bintray repository.
76
113
77
- These libraries require kotlin compiler version to be at least ` 1.1.0 ` and
78
- require kotlin runtime of the same version as a dependency.
114
+ The library depends on the Kotlin Standard Library of the version at least ` 1.3.30 ` .
79
115
80
116
### Maven
81
117
@@ -98,7 +134,7 @@ Add dependencies (you can also add other modules that you need):
98
134
<dependency >
99
135
<groupId >org.jetbrains.kotlinx</groupId >
100
136
<artifactId >kotlinx-collections-immutable</artifactId >
101
- <version >0.1 </version >
137
+ <version >0.2 </version >
102
138
</dependency >
103
139
```
104
140
@@ -117,7 +153,7 @@ repositories {
117
153
Add the dependency:
118
154
119
155
``` groovy
120
- compile 'org.jetbrains.kotlinx:kotlinx-collections-immutable:0.1 '
156
+ compile 'org.jetbrains.kotlinx:kotlinx-collections-immutable:0.2 '
121
157
```
122
158
123
159
0 commit comments