Skip to content

Commit c18905e

Browse files
authored
docs: Update README with comprehensive iterator/sequence function documentation (#4)
1 parent cc1d509 commit c18905e

File tree

2 files changed

+134
-25
lines changed

2 files changed

+134
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ go.work.sum
2323

2424
# env file
2525
.env
26+
.aider*

README.md

Lines changed: 133 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,136 @@
66

77
Golang's "missing" iterator/sequence functions.
88

9-
## iter.Seq helpers
10-
11-
* `With(...T) iter.Seq[T]` : Construct a sequence using the provided values;
12-
* `FromChan(<-chan) iter.Seq[T]`: Returns a sequence that produces values until the channel is closed;
13-
* `ToChan(iter.Seq[T]) <-chan`: Returns a channel that produces values until the sequence is exhausted;
14-
* `ToChanCtx(context.Context, iter.Seq[T]) <-chan`: Returns a channel that produces values until the sequence is
15-
exhausted or the context is canceled;
16-
* `Map(iter.Seq[T], func(T) O) iter.Seq[O]`: Maps the items in the sequence to another type;
17-
* `Append(iter.Seq[T], ...T) iter.Seq[T]`: Returns a new sequence that includes the items from the passed sequence, plus
18-
the additional items;
19-
* `Filter(iter.Seq[T], func(T) bool) iter.Seq[T]`: Filter the values in the sequence by applying fn to each value;
20-
* `IterKV(iter.Seq[T], func(V) K) iter.Seq2[K,V]`: ...
21-
22-
## iter.Seq2 helpers
23-
24-
Some of these helpers use seq.KV, some do not. I've generally tried to use seq.KV when it would:
25-
26-
1. easily be confusing to deal with keys and values as separate values
27-
2. when they have to be paired together to avoid having to handle odd numbers of input
28-
29-
* `WithKV(...KV[K,V])` : Construct a key-value (iter.Seq2) sequence using the provided key-values;
30-
* `MapKV(iter.Seq2[K,V], func(K,V) (K1,V1)) iter.Seq2[K1,V2]`: Maps the items in the sequence to other types;
31-
* `AppendKV(iter.Seq2[K,V], ...KV[K,V]) iter.Seq2[K,V]`: Returns a new sequence that includes the items from the passed sequence, plus
32-
the additional KV pairs;
33-
* `FilterKV(iter.Seq2[K,V], func(K,V) bool) iter.Seq2[K,V]`: Filter the values in the sequence by applying fn to each value;
9+
## Construction Functions
10+
11+
### iter.Seq[T]
12+
13+
* `With(...T) iter.Seq[T]`: Construct a sequence using the provided values
14+
* `FromChan(<-chan T) iter.Seq[T]`: Returns a sequence that produces values until the channel is closed
15+
* `Repeat(int, T) iter.Seq[T]`: Returns a sequence which repeats the value n times
16+
17+
### iter.Seq2[K,V]
18+
19+
* `WithKV(...KV[K,V]) iter.Seq2[K,V]`: Construct a key-value sequence using the provided key-values
20+
* `RepeatKV(int, K, V) iter.Seq2[K,V]`: Returns a sequence which repeats the key-value pair n times
21+
22+
## Conversion Functions
23+
24+
* `ToChan(iter.Seq[T]) <-chan T`: Returns a channel that produces values until the sequence is exhausted
25+
* `ToChanCtx(context.Context, iter.Seq[T]) <-chan T`: Returns a channel that produces values until the sequence is exhausted or the context is canceled
26+
* `IterKV(iter.Seq[V], func(V) K) iter.Seq2[K,V]`: Converts an iter.Seq[V] to an iter.Seq2[K,V] using keyFn for keys
27+
* `IterK(iter.Seq2[K,V]) iter.Seq[K]`: Converts an iter.Seq2[K,V] to an iter.Seq[K] (keys only)
28+
* `IterV(iter.Seq2[K,V]) iter.Seq[V]`: Converts an iter.Seq2[K,V] to an iter.Seq[V] (values only)
29+
* `MapToKV(iter.Seq[T], func(T) (K,V)) iter.Seq2[K,V]`: Maps values to key-value pairs
30+
31+
## Transformation Functions
32+
33+
### Mapping
34+
35+
* `Map(iter.Seq[T], func(T) O) iter.Seq[O]`: Maps the items in the sequence to another type
36+
* `MapKV(iter.Seq2[K,V], func(K,V) (K1,V1)) iter.Seq2[K1,V1]`: Maps the key-value pairs to other types
37+
38+
### Filtering
39+
40+
* `Filter(iter.Seq[T], func(T) bool) iter.Seq[T]`: Filter values by applying fn to each value
41+
* `FilterKV(iter.Seq2[K,V], func(K,V) bool) iter.Seq2[K,V]`: Filter key-value pairs by applying fn to each pair
42+
43+
### Appending
44+
45+
* `Append(iter.Seq[T], ...T) iter.Seq[T]`: Returns a new sequence with additional items appended
46+
* `AppendKV(iter.Seq2[K,V], ...KV[K,V]) iter.Seq2[K,V]`: Returns a new sequence with additional key-value pairs appended
47+
48+
### Replacement
49+
50+
* `Replace(iter.Seq[T], old, new T) iter.Seq[T]`: Replace old values with new values
51+
* `ReplaceKV(iter.Seq2[K,V], old, new KV[K,V]) iter.Seq2[K,V]`: Replace old key-value pairs with new ones
52+
53+
### Compacting
54+
55+
* `Compact(iter.Seq[T]) iter.Seq[T]`: Yields all values that are not equal to the previous value
56+
* `CompactFunc(iter.Seq[T], func(T,T) bool) iter.Seq[T]`: Like Compact but uses a function to compare elements
57+
* `CompactKV(iter.Seq2[K,V]) iter.Seq2[K,V]`: Yields all key-value pairs that are not equal to the previous pair
58+
* `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
59+
60+
### Chunking
61+
62+
* `Chunk(iter.Seq[T], int) iter.Seq[iter.Seq[T]]`: Chunk the sequence into chunks of specified size
63+
* `ChunkKV(iter.Seq2[K,V], int) iter.Seq[iter.Seq2[K,V]]`: Chunk key-value pairs into chunks of specified size
64+
65+
### Dropping
66+
67+
* `Drop(iter.Seq[T], int) iter.Seq[T]`: Drop n elements from the start of the sequence
68+
* `DropKV(iter.Seq2[K,V], int) iter.Seq2[K,V]`: Drop n key-value pairs from the start of the sequence
69+
* `DropBy(iter.Seq[T], func(T) bool) iter.Seq[T]`: Drop all elements for which the function returns true
70+
* `DropKVBy(iter.Seq2[K,V], func(K,V) bool) iter.Seq2[K,V]`: Drop all key-value pairs for which the function returns true
71+
72+
## Aggregation Functions
73+
74+
### Min/Max
75+
76+
* `Min(iter.Seq[T]) (T, bool)`: Min value from the sequence using built-in comparison
77+
* `MinFunc(iter.Seq[T], func(T,T) int) (T, bool)`: Min value using a comparison function
78+
* `MinFuncKV(iter.Seq2[K,V], func(KV[K,V], KV[K,V]) int) (KV[K,V], bool)`: Min key-value pair using a comparison function
79+
* `Max(iter.Seq[T]) (T, bool)`: Max value from the sequence using built-in comparison
80+
* `MaxFunc(iter.Seq[T], func(T,T) int) (T, bool)`: Max value using a comparison function
81+
* `MaxFuncKV(iter.Seq2[K,V], func(KV[K,V], KV[K,V]) int) (KV[K,V], bool)`: Max key-value pair using a comparison function
82+
83+
### Reduction
84+
85+
* `Reduce(iter.Seq[T], O, func(O,T) O) O`: Reduce the sequence to a single value
86+
* `ReduceKV(iter.Seq2[K,V], O, func(O,K,V) O) O`: Reduce key-value pairs to a single value
87+
88+
### Counting
89+
90+
* `Count(iter.Seq[T]) int`: Returns the number of elements in the sequence
91+
* `CountKV(iter.Seq2[K,V]) int`: Returns the number of key-value pairs in the sequence
92+
* `CountBy(iter.Seq[T], func(T) bool) int`: Count elements for which the function returns true
93+
* `CountKVBy(iter.Seq2[K,V], func(K,V) bool) int`: Count key-value pairs for which the function returns true
94+
* `CountValues(iter.Seq[T]) iter.Seq2[T,int]`: Returns a sequence where keys are values and values are their counts
95+
96+
## Comparison Functions
97+
98+
* `Compare(iter.Seq[T], iter.Seq[T]) int`: Compare two sequences using cmp.Compare
99+
* `CompareFunc(iter.Seq[T], iter.Seq[T], func(T,T) int) int`: Compare two sequences using a comparison function
100+
* `CompareKV(iter.Seq2[K,V], iter.Seq2[K,V]) int`: Compare two key-value sequences using cmp.Compare
101+
* `CompareKVFunc(iter.Seq2[AK,AV], iter.Seq2[BK,BV], func(KV[AK,AV], KV[BK,BV]) int) int`: Compare two key-value sequences using a comparison function
102+
103+
## Equality Functions
104+
105+
* `Equal(iter.Seq[T], iter.Seq[T]) bool`: Returns true if sequences are equal
106+
* `EqualKV(iter.Seq2[K,V], iter.Seq2[K,V]) bool`: Returns true if key-value sequences are equal
107+
* `EqualFunc(iter.Seq[T], iter.Seq[T], func(T,T) bool) bool`: Test equality using a comparison function
108+
* `EqualKVFunc(iter.Seq2[AK,AV], iter.Seq2[BK,BV], func(KV[AK,AV], KV[BK,BV]) bool) bool`: Test key-value equality using a comparison function
109+
110+
## Search Functions
111+
112+
### Contains
113+
114+
* `Contains(iter.Seq[T], T) bool`: Returns true if the value is in the sequence
115+
* `ContainsKV(iter.Seq2[K,V], K, V) bool`: Returns true if the key-value pair is in the sequence
116+
* `ContainsFunc(iter.Seq[T], func(T) bool) bool`: Returns true if predicate returns true for any value
117+
* `ContainsKVFunc(iter.Seq2[K,V], func(K,V) bool) bool`: Returns true if predicate returns true for any key-value pair
118+
119+
### Finding
120+
121+
* `Find(iter.Seq[T], T) (int, bool)`: Returns the index of the first occurrence of the value
122+
* `FindBy(iter.Seq[T], func(T) bool) (T, int, bool)`: Returns the first value for which the function returns true
123+
* `FindByKey(iter.Seq2[K,V], K) (V, int, bool)`: Returns the value of the first key-value pair with the given key
124+
* `FindByValue(iter.Seq2[K,V], V) (K, int, bool)`: Returns the key of the first key-value pair with the given value
125+
126+
## Utility Functions
127+
128+
* `Coalesce(iter.Seq[T]) (T, bool)`: Returns the first non-zero value in the sequence
129+
* `CoalesceKV(iter.Seq2[K,V]) (KV[K,V], bool)`: Returns the first key-value pair with a non-zero value
130+
* `IsSorted(iter.Seq[T]) bool`: Returns true if the sequence is sorted
131+
* `IsSortedKV(iter.Seq2[K,V]) bool`: Returns true if the key-value sequence is sorted
132+
* `IntK() func(V) int`: Returns a function that generates increasing integers starting at 0
133+
134+
## Time-based Functions
135+
136+
* `EveryUntil(time.Duration, time.Time) iter.Seq[time.Time]`: Yields time every duration until the specified time
137+
* `EveryN(time.Duration, int) iter.Seq[time.Time]`: Yields time every duration for n times
138+
139+
## Types
140+
141+
* `KV[K,V]`: A struct that pairs a key and value together for use with key-value sequence functions

0 commit comments

Comments
 (0)