diff options
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 62 |
1 files changed, 31 insertions, 31 deletions
@@ -2,7 +2,7 @@ Immutable [ -team as the `List` & `Map` implementations are loose ports from that project. +team as the `Vector` & `Map` implementations are loose ports from that project. -## List +## Vector -The `List` type represents a sorted, indexed collection of values and operates +The `Vector` type represents a sorted, indexed collection of values and operates similarly to a Go slice. It supports efficient append, prepend, update, and slice operations. -### Adding list elements +### Adding vector elements -Elements can be added to the end of the list with the `Append()` method or added -to the beginning of the list with the `Prepend()` method. Unlike Go slices, +Elements can be added to the end of the vector with the `Append()` method or added +to the beginning of the vector with the `Prepend()` method. Unlike Go slices, prepending is as efficient as appending. ```go -// Create a list with 3 elements. -l := immutable.NewList[string]() +// Create a vector with 3 elements. +l := immutable.NewVector[string]() l = l.Append("foo") l = l.Append("bar") l = l.Prepend("baz") @@ -45,30 +45,30 @@ fmt.Println(l.Get(1)) // "foo" fmt.Println(l.Get(2)) // "bar" ``` -Note that each change to the list results in a new list being created. These -lists are all snapshots at that point in time and cannot be changed so they +Note that each change to the vector results in a new vector being created. These +vectors are all snapshots at that point in time and cannot be changed so they are safe to share between multiple goroutines. -### Updating list elements +### Updating vector elements You can also overwrite existing elements by using the `Set()` method. In the -following example, we'll update the third element in our list and return the -new list to a new variable. You can see that our old `l` variable retains a +following example, we'll update the third element in our vector and return the +new vector to a new variable. You can see that our old `l` variable retains a snapshot of the original value. ```go -l := immutable.NewList[string]() +l := immutable.NewVector[string]() l = l.Append("foo") l = l.Append("bar") -newList := l.Set(2, "baz") +newVector := l.Set(2, "baz") fmt.Println(l.Get(1)) // "bar" -fmt.Println(newList.Get(1)) // "baz" +fmt.Println(newVector.Get(1)) // "baz" ``` -### Deriving sublists +### Deriving subvectors -You can create a sublist by using the `Slice()` method. This method works with +You can create a subvector by using the `Slice()` method. This method works with the same rules as subslicing a Go slice: ```go @@ -79,18 +79,18 @@ fmt.Println(l.Get(0)) // "baz" fmt.Println(l.Get(1)) // "foo" ``` -Please note that since `List` follows the same rules as slices, it will panic if +Please note that since `Vector` follows the same rules as slices, it will panic if you try to `Get()`, `Set()`, or `Slice()` with indexes that are outside of -the range of the `List`. +the range of the `Vector`. -### Iterating lists +### Iterating vectors -Iterators provide a clean, simple way to iterate over the elements of the list +Iterators provide a clean, simple way to iterate over the elements of the vector in order. This is more efficient than simply calling `Get()` for each index. -Below is an example of iterating over all elements of our list from above: +Below is an example of iterating over all elements of our vector from above: ```go itr := l.Iterator() @@ -107,25 +107,25 @@ By default iterators start from index zero, however, the `Seek()` method can be used to jump to a given index. -### Efficiently building lists +### Efficiently building vectors -If you are building large lists, it is significantly more efficient to use the -`ListBuilder`. It uses nearly the same API as `List` except that it updates -a list in-place until you are ready to use it. This can improve bulk list +If you are building large vectors, it is significantly more efficient to use the +`VectorBuilder`. It uses nearly the same API as `Vector` except that it updates +a vector in-place until you are ready to use it. This can improve bulk vector building by 10x or more. ```go -b := immutable.NewListBuilder[string]() +b := immutable.NewVectorBuilder[string]() b.Append("foo") b.Append("bar") b.Set(2, "baz") -l := b.List() +l := b.Vector() fmt.Println(l.Get(0)) // "foo" fmt.Println(l.Get(1)) // "baz" ``` -Builders are invalid after the call to `List()`. +Builders are invalid after the call to `Vector()`. ## Map |