Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Golang's "missing" iterator/sequence functions.

* `With(...T) iter.Seq[T]`: Construct a sequence using the provided values
* `FromChan(<-chan T) iter.Seq[T]`: Returns a sequence that produces values until the channel is closed
* `FromChanCtx(context.Context, <-chan T) iter.Seq[T]`: Like FromChan but also stops when the context is canceled
* `Repeat(int, T) iter.Seq[T]`: Returns a sequence which repeats the value n times

### iter.Seq2[K,V]
Expand All @@ -27,13 +28,20 @@ Golang's "missing" iterator/sequence functions.
* `IterK(iter.Seq2[K,V]) iter.Seq[K]`: Converts an iter.Seq2[K,V] to an iter.Seq[K] (keys only)
* `IterV(iter.Seq2[K,V]) iter.Seq[V]`: Converts an iter.Seq2[K,V] to an iter.Seq[V] (values only)
* `MapToKV(iter.Seq[T], func(T) (K,V)) iter.Seq2[K,V]`: Maps values to key-value pairs
* `SwapKV(iter.Seq2[K,V]) iter.Seq2[V,K]`: Swaps the keys and values of each pair
* `Enumerate(iter.Seq[T]) iter.Seq2[int,T]`: Pairs each value with its 0-based index; the index restarts on each iteration

## Transformation Functions

### Mapping

* `Map(iter.Seq[T], func(T) O) iter.Seq[O]`: Maps the items in the sequence to another type
* `MapKV(iter.Seq2[K,V], func(K,V) (K1,V1)) iter.Seq2[K1,V1]`: Maps the key-value pairs to other types
* `FlatMap(iter.Seq[T], func(T) iter.Seq[O]) iter.Seq[O]`: Maps each value to a sequence and yields the elements of each in order
* `Scan(iter.Seq[T], O, func(O,T) O) iter.Seq[O]`: Like Reduce but lazily yields the accumulated value after each element
* `ScanKV(iter.Seq2[K,V], O, func(O,K,V) O) iter.Seq[O]`: Like ReduceKV but lazily yields the accumulated value after each pair
* `Tap(iter.Seq[T], func(T)) iter.Seq[T]`: Yields the same elements, calling the function on each as it passes through
* `TapKV(iter.Seq2[K,V], func(K,V)) iter.Seq2[K,V]`: Yields the same pairs, calling the function on each as it passes through

### Filtering

Expand All @@ -45,6 +53,19 @@ Golang's "missing" iterator/sequence functions.
* `Append(iter.Seq[T], ...T) iter.Seq[T]`: Returns a new sequence with additional items appended
* `AppendKV(iter.Seq2[K,V], ...KV[K,V]) iter.Seq2[K,V]`: Returns a new sequence with additional key-value pairs appended

### Combining

* `Concat(...iter.Seq[T]) iter.Seq[T]`: Yields the elements of each sequence in order
* `ConcatKV(...iter.Seq2[K,V]) iter.Seq2[K,V]`: Yields the key-value pairs of each sequence in order
* `Zip(iter.Seq[A], iter.Seq[B]) iter.Seq2[A,B]`: Pairs the elements of two sequences positionally, ending at the shorter one
* `Merge(iter.Seq[T], iter.Seq[T]) iter.Seq[T]`: Merges two sorted sequences into one sorted sequence
* `MergeFunc(iter.Seq[T], iter.Seq[T], func(T,T) int) iter.Seq[T]`: Like Merge but uses a comparison function

### Cycling

* `Cycle(iter.Seq[T]) iter.Seq[T]`: Repeats the sequence forever (empty input yields an empty sequence)
* `CycleKV(iter.Seq2[K,V]) iter.Seq2[K,V]`: Repeats the key-value sequence forever (empty input yields an empty sequence)

### Replacement

* `Replace(iter.Seq[T], old, new T) iter.Seq[T]`: Replace old values with new values
Expand All @@ -56,18 +77,39 @@ Golang's "missing" iterator/sequence functions.
* `CompactFunc(iter.Seq[T], func(T,T) bool) iter.Seq[T]`: Like Compact but uses a function to compare elements
* `CompactKV(iter.Seq2[K,V]) iter.Seq2[K,V]`: Yields all key-value pairs that are not equal to the previous pair
* `CompactKVFunc(iter.Seq2[K,V], func(KV[K,V], KV[K,V]) bool) iter.Seq2[K,V]`: Like CompactKV but uses a function to compare pairs
* `Unique(iter.Seq[T]) iter.Seq[T]`: Yields the first occurrence of each distinct value (removes duplicates anywhere, not just adjacent)
* `UniqueKV(iter.Seq2[K,V]) iter.Seq2[K,V]`: Yields the first occurrence of each distinct key-value pair

### Chunking

* `Chunk(iter.Seq[T], int) iter.Seq[iter.Seq[T]]`: Chunk the sequence into chunks of specified size
* `ChunkKV(iter.Seq2[K,V], int) iter.Seq[iter.Seq2[K,V]]`: Chunk key-value pairs into chunks of specified size
* `Windows(iter.Seq[T], int) iter.Seq[iter.Seq[T]]`: Overlapping windows of the specified size (sliding by one element)
* `WindowsKV(iter.Seq2[K,V], int) iter.Seq[iter.Seq2[K,V]]`: Overlapping windows of key-value pairs
* `Flatten(iter.Seq[iter.Seq[T]]) iter.Seq[T]`: Yields the elements of each inner sequence in order (the inverse of Chunk)
* `FlattenKV(iter.Seq[iter.Seq2[K,V]]) iter.Seq2[K,V]`: Yields the key-value pairs of each inner sequence in order (the inverse of ChunkKV)

### Grouping

* `GroupBy(iter.Seq[T], func(T) K) iter.Seq2[K,[]T]`: Groups values by key in first-seen order
* `Partition(iter.Seq[T], func(T) bool) (iter.Seq[T], iter.Seq[T])`: Splits into matching and non-matching sequences
* `PartitionKV(iter.Seq2[K,V], func(K,V) bool) (iter.Seq2[K,V], iter.Seq2[K,V])`: Splits key-value pairs into matching and non-matching sequences

### Taking

* `Take(iter.Seq[T], int) iter.Seq[T]`: Take the first n elements of the sequence
* `TakeKV(iter.Seq2[K,V], int) iter.Seq2[K,V]`: Take the first n key-value pairs of the sequence
* `TakeWhile(iter.Seq[T], func(T) bool) iter.Seq[T]`: Take leading elements while the function returns true
* `TakeKVWhile(iter.Seq2[K,V], func(K,V) bool) iter.Seq2[K,V]`: Take leading key-value pairs while the function returns true

### Dropping

* `Drop(iter.Seq[T], int) iter.Seq[T]`: Drop n elements from the start of the sequence
* `DropKV(iter.Seq2[K,V], int) iter.Seq2[K,V]`: Drop n key-value pairs from the start of the sequence
* `DropBy(iter.Seq[T], func(T) bool) iter.Seq[T]`: Drop all elements for which the function returns true
* `DropKVBy(iter.Seq2[K,V], func(K,V) bool) iter.Seq2[K,V]`: Drop all key-value pairs for which the function returns true
* `DropWhile(iter.Seq[T], func(T) bool) iter.Seq[T]`: Drop leading elements while the function returns true, then yield the rest
* `DropKVWhile(iter.Seq2[K,V], func(K,V) bool) iter.Seq2[K,V]`: Drop leading key-value pairs while the function returns true, then yield the rest

## Aggregation Functions

Expand All @@ -85,6 +127,12 @@ Golang's "missing" iterator/sequence functions.
* `Reduce(iter.Seq[T], O, func(O,T) O) O`: Reduce the sequence to a single value
* `ReduceKV(iter.Seq2[K,V], O, func(O,K,V) O) O`: Reduce key-value pairs to a single value

### Numeric

* `Sum(iter.Seq[T]) T`: Sum of the values (zero for an empty sequence); T is any integer or float type
* `Product(iter.Seq[T]) T`: Product of the values (one for an empty sequence); T is any integer or float type
* `Average(iter.Seq[T]) (float64, bool)`: Arithmetic mean of the values; false if the sequence is empty

### Counting

* `Count(iter.Seq[T]) int`: Returns the number of elements in the sequence
Expand Down Expand Up @@ -116,6 +164,13 @@ Golang's "missing" iterator/sequence functions.
* `ContainsFunc(iter.Seq[T], func(T) bool) bool`: Returns true if predicate returns true for any value
* `ContainsKVFunc(iter.Seq2[K,V], func(K,V) bool) bool`: Returns true if predicate returns true for any key-value pair

### Predicates

* `All(iter.Seq[T], func(T) bool) bool`: Returns true if the function returns true for every value (true for empty)
* `AllKV(iter.Seq2[K,V], func(K,V) bool) bool`: Returns true if the function returns true for every key-value pair (true for empty)
* `None(iter.Seq[T], func(T) bool) bool`: Returns true if the function returns false for every value (true for empty)
* `NoneKV(iter.Seq2[K,V], func(K,V) bool) bool`: Returns true if the function returns false for every key-value pair (true for empty)

### Finding

* `Find(iter.Seq[T], T) (int, bool)`: Returns the index of the first occurrence of the value
Expand All @@ -124,6 +179,8 @@ Golang's "missing" iterator/sequence functions.
* `FindByValue(iter.Seq2[K,V], V) (K, int, bool)`: Returns the key of the first key-value pair with the given value
* `At(iter.Seq[T], int) (T, bool)`: Returns the value at the given 0-based index, or zero value and false if out of range
* `AtKV(iter.Seq2[K,V], int) (K, V, bool)`: Returns the key and value at the given 0-based index, or zero values and false if out of range
* `Last(iter.Seq[T]) (T, bool)`: Returns the final value in the sequence, or zero value and false if empty
* `LastKV(iter.Seq2[K,V]) (K, V, bool)`: Returns the final key-value pair in the sequence, or zero values and false if empty

## Utility Functions

Expand All @@ -141,3 +198,4 @@ Golang's "missing" iterator/sequence functions.
## Types

* `KV[K,V]`: A struct that pairs a key and value together for use with key-value sequence functions
* `Number`: A constraint permitting any integer or floating point type, used by Sum, Product, and Average
Loading
Loading