|
1 | 1 | # Immutable Collections Library for Kotlin
|
2 | 2 |
|
3 |
| -Immutable collection interfaces and implementation prototypes for Kotlin ([proposal](proposal.md)) |
| 3 | +Immutable collection interfaces and implementation prototypes for Kotlin. |
| 4 | + |
| 5 | +For further details see the [proposal](proposal.md). |
4 | 6 |
|
5 | 7 | Prototype implementation is based on [pcollections](http://pcollections.org/) (Copyright 2015 The pcollections Authors.)
|
6 | 8 |
|
| 9 | +## What's in this library |
| 10 | +### Interfaces and implementations |
| 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` |
| 22 | +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. |
| 24 | + |
| 25 | +### Operations |
| 26 | + |
| 27 | +#### toImmutableList/Set/Map |
| 28 | +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. |
| 30 | + |
| 31 | + fun Iterable<T>.toImmutableList(): ImmutableList<T> |
| 32 | + fun Iterable<T>.toImmutableSet(): ImmutableSet<T> |
| 33 | + |
| 34 | +#### `+` and `-` operators |
| 35 | + |
| 36 | +`plus` and `minus` operators on immutable collections exploit their immutability |
| 37 | +and delegate the implementation to the collections themselves. |
| 38 | +The operation is performed with persistence in mind: the returned immutable collection may share storage |
| 39 | +with the original collection. |
| 40 | + |
| 41 | +```kotlin |
| 42 | +val newList = immutableListOf("a", "b") + "c" |
| 43 | +// newList is also ImmutableList |
| 44 | +``` |
| 45 | + |
| 46 | +> **Note:** you need to import these operators from `kotlinx.collections.immutable` package |
| 47 | +in order for them to take the precedence over the ones from the |
| 48 | +standard library. |
| 49 | + |
| 50 | +``` |
| 51 | +import kotlinx.collections.immutable.* |
| 52 | +``` |
| 53 | + |
| 54 | +#### Mutate |
| 55 | + |
| 56 | +`mutate` extension function simplifies quite common pattern of immutable collection modification: |
| 57 | +get a builder, apply some mutating operations on it, transform it back to an immutable collection: |
| 58 | + |
| 59 | + collection.builder().apply { some_actions_on(this) }.build() |
| 60 | + |
| 61 | +With `mutate` it transforms to: |
7 | 62 |
|
| 63 | + collection.mutate { some_actions_on(it) } |
8 | 64 |
|
9 | 65 | ## Using in your projects
|
10 | 66 |
|
|
0 commit comments