Skip to content

Commit a43847f

Browse files
committed
Provide brief overview of the public API.
1 parent bf8b3a6 commit a43847f

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,66 @@
11
# Immutable Collections Library for Kotlin
22

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).
46

57
Prototype implementation is based on [pcollections](http://pcollections.org/) (Copyright 2015 The pcollections Authors.)
68

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:
762

63+
collection.mutate { some_actions_on(it) }
864

965
## Using in your projects
1066

0 commit comments

Comments
 (0)