mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
doc: documentating every helpers of "it" sub-package
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
---
|
||||
name: Assign
|
||||
slug: assign
|
||||
sourceRef: it/map.go#L122
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func Assign[K comparable, V any, Map ~map[K]V](maps ...iter.Seq[Map]) Map"
|
||||
variantHelpers:
|
||||
- it#map#assign
|
||||
similarHelpers:
|
||||
- core#map#assign
|
||||
- it#map#fromentries
|
||||
- it#map#invert
|
||||
position: 50
|
||||
---
|
||||
|
||||
Merges multiple map sequences into a single map. Later maps overwrite values from earlier maps when keys conflict.
|
||||
|
||||
```go
|
||||
map1 := func(yield func(map[string]int) bool) {
|
||||
yield(map[string]int{"a": 1, "b": 2})
|
||||
}
|
||||
map2 := func(yield func(map[string]int) bool) {
|
||||
yield(map[string]int{"b": 3, "c": 4})
|
||||
}
|
||||
map3 := func(yield func(map[string]int) bool) {
|
||||
yield(map[string]int{"d": 5, "e": 6})
|
||||
}
|
||||
result := lo.Assign(map1, map2, map3)
|
||||
// map[string]int{"a": 1, "b": 3, "c": 4, "d": 5, "e": 6}
|
||||
// Note: "b" is 3 (overwritten from map2)
|
||||
|
||||
singleMap := func(yield func(map[int]string) bool) {
|
||||
yield(map[int]string{1: "one", 2: "two"})
|
||||
}
|
||||
result = lo.Assign(singleMap)
|
||||
// map[int]string{1: "one", 2: "two"}
|
||||
|
||||
emptyMap1 := func(yield func(map[string]bool) bool) {
|
||||
yield(map[string]bool{})
|
||||
}
|
||||
emptyMap2 := func(yield func(map[string]bool) bool) {
|
||||
yield(map[string]bool{"active": true})
|
||||
}
|
||||
result = lo.Assign(emptyMap1, emptyMap2)
|
||||
// map[string]bool{"active": true}
|
||||
```
|
||||
@@ -0,0 +1,90 @@
|
||||
---
|
||||
name: Associate
|
||||
slug: associate
|
||||
sourceRef: it/seq.go#L412
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func Associate[T any, K comparable, V any](collection iter.Seq[T], transform func(item T) (K, V)) map[K]V"
|
||||
- "func AssociateI[T any, K comparable, V any](collection iter.Seq[T], transform func(item T, index int) (K, V)) map[K]V"
|
||||
- "func SeqToMap[T any, K comparable, V any](collection iter.Seq[T], transform func(item T) (K, V)) map[K]V"
|
||||
- "func SeqToMapI[T any, K comparable, V any](collection iter.Seq[T], transform func(item T, index int) (K, V)) map[K]V"
|
||||
- "func FilterSeqToMap[T any, K comparable, V any](collection iter.Seq[T], transform func(item T) (K, V, bool)) map[K]V"
|
||||
- "func FilterSeqToMapI[T any, K comparable, V any](collection iter.Seq[T], transform func(item T, index int) (K, V, bool)) map[K]V"
|
||||
variantHelpers:
|
||||
- it#sequence#associate
|
||||
- it#sequence#associatei
|
||||
- it#sequence#seqtomap
|
||||
- it#sequence#seqtomapi
|
||||
- it#sequence#filterseqtomap
|
||||
- it#sequence#filterseqtomapi
|
||||
similarHelpers:
|
||||
- core#slice#associate
|
||||
- core#map#keyby
|
||||
- it#map#keyby
|
||||
position: 140
|
||||
---
|
||||
|
||||
Associate returns a map containing key-value pairs provided by transform function applied to elements of the given sequence.
|
||||
If any of two pairs have the same key the last one gets added to the map.
|
||||
|
||||
```go
|
||||
collection := func(yield func(string) bool) {
|
||||
yield("apple")
|
||||
yield("banana")
|
||||
yield("cherry")
|
||||
}
|
||||
|
||||
result := it.Associate(collection, func(s string) (string, int) {
|
||||
return s, len(s)
|
||||
})
|
||||
// result contains {"apple": 5, "banana": 6, "cherry": 6}
|
||||
```
|
||||
|
||||
AssociateI returns a map containing key-value pairs provided by transform function applied to elements of the given sequence, with index.
|
||||
|
||||
```go
|
||||
collection := func(yield func(string) bool) {
|
||||
yield("a")
|
||||
yield("b")
|
||||
yield("c")
|
||||
}
|
||||
|
||||
result := it.AssociateI(collection, func(item string, index int) (int, string) {
|
||||
return index, item
|
||||
})
|
||||
// result contains {0: "a", 1: "b", 2: "c"}
|
||||
```
|
||||
|
||||
SeqToMap returns a map containing key-value pairs provided by transform function applied to elements of the given sequence.
|
||||
Alias of Associate().
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
}
|
||||
|
||||
result := it.SeqToMap(collection, func(i int) (int, string) {
|
||||
return i, fmt.Sprintf("item-%d", i)
|
||||
})
|
||||
// result contains {1: "item-1", 2: "item-2", 3: "item-3"}
|
||||
```
|
||||
|
||||
FilterSeqToMap returns a map containing key-value pairs provided by transform function applied to elements of the given sequence.
|
||||
The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
}
|
||||
|
||||
result := it.FilterSeqToMap(collection, func(i int) (int, string, bool) {
|
||||
return i, fmt.Sprintf("item-%d", i), i%2 == 0
|
||||
})
|
||||
// result contains {2: "item-2", 4: "item-4"}
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
name: ChannelToSeq
|
||||
slug: channeltoseq
|
||||
sourceRef: it/channel.go#L39
|
||||
category: it
|
||||
subCategory: channel
|
||||
signatures:
|
||||
- "func ChannelToSeq[T any](ch <-chan T) iter.Seq[T]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#channel#channeltoseq
|
||||
similarHelpers:
|
||||
- it#channel#seqtochannel
|
||||
position: 10
|
||||
---
|
||||
|
||||
Builds an `iter.Seq` from a channel. The returned sequence yields each value received from the channel in order. Iteration blocks until the channel is closed.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
ch := make(chan int, 3)
|
||||
ch <- 1; ch <- 2; ch <- 3
|
||||
close(ch)
|
||||
|
||||
seq := it.ChannelToSeq(ch)
|
||||
var got []int
|
||||
for v := range seq {
|
||||
got = append(got, v)
|
||||
}
|
||||
// got == []int{1, 2, 3}
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
---
|
||||
name: SeqToChannel2
|
||||
slug: channeltoseq
|
||||
sourceRef: it/channel.go#L26
|
||||
category: it
|
||||
subCategory: channel
|
||||
signatures:
|
||||
- "func SeqToChannel2[K, V any](bufferSize int, collection iter.Seq2[K, V]) <-chan lo.Tuple2[K, V]"
|
||||
- "func ChannelToSeq[T any](ch <-chan T) iter.Seq[T]"
|
||||
variantHelpers:
|
||||
- it#channel#seqtochannel
|
||||
- it#channel#seqtochannel2
|
||||
- it#channel#channeltoseq
|
||||
similarHelpers:
|
||||
- core#channel#channelseq
|
||||
position: 10
|
||||
---
|
||||
|
||||
SeqToChannel2 returns a read-only channel of key-value tuple elements from a sequence.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int, string) bool) {
|
||||
yield(1, "a")
|
||||
yield(2, "b")
|
||||
}
|
||||
|
||||
ch := it.SeqToChannel2(10, collection)
|
||||
for tuple := range ch {
|
||||
fmt.Printf("%d: %s\n", tuple.A, tuple.B)
|
||||
}
|
||||
// 1: a
|
||||
// 2: b
|
||||
```
|
||||
|
||||
ChannelToSeq returns a sequence built from channel items. Blocks until channel closes.
|
||||
|
||||
```go
|
||||
ch := make(chan int, 3)
|
||||
ch <- 1
|
||||
ch <- 2
|
||||
ch <- 3
|
||||
close(ch)
|
||||
|
||||
seq := it.ChannelToSeq(ch)
|
||||
for item := range seq {
|
||||
fmt.Println(item)
|
||||
}
|
||||
// 1
|
||||
// 2
|
||||
// 3
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
name: Chunk
|
||||
slug: chunk
|
||||
sourceRef: it/seq.go#L264
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Chunk[T any](collection iter.Seq[T], size int) iter.Seq[[]T]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#sequence#chunk
|
||||
similarHelpers:
|
||||
- core#slice#chunk
|
||||
- it#sequence#partitionby
|
||||
position: 60
|
||||
---
|
||||
|
||||
Returns a sequence of elements split into groups of length size. The last chunk may be smaller than size.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
_ = yield(5)
|
||||
}
|
||||
chunks := it.Chunk(seq, 2)
|
||||
var result [][]int
|
||||
for chunk := range chunks {
|
||||
result = append(result, chunk)
|
||||
}
|
||||
// result contains [1, 2], [3, 4], [5]
|
||||
```
|
||||
@@ -0,0 +1,42 @@
|
||||
---
|
||||
name: ChunkEntries
|
||||
slug: chunkentries
|
||||
sourceRef: it/map.go#L138
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func ChunkEntries[K comparable, V any](m map[K]V, size int) iter.Seq[map[K]V]"
|
||||
variantHelpers:
|
||||
- it#map#chunkentries
|
||||
similarHelpers:
|
||||
- core#map#chunkentries
|
||||
- it#sequence#chunk
|
||||
- it#map#keys
|
||||
- it#map#values
|
||||
position: 60
|
||||
---
|
||||
|
||||
Chunks a map into smaller maps of the specified size. Returns a sequence of maps, each containing up to the specified number of entries.
|
||||
|
||||
```go
|
||||
originalMap := map[string]int{
|
||||
"a": 1, "b": 2, "c": 3, "d": 4, "e": 5,
|
||||
}
|
||||
result := lo.ChunkEntries(originalMap, 2)
|
||||
// iter.Seq[map[string]int] yielding:
|
||||
// map[string]int{"a": 1, "b": 2}
|
||||
// map[string]int{"c": 3, "d": 4}
|
||||
// map[string]int{"e": 5}
|
||||
|
||||
smallMap := map[int]string{1: "one", 2: "two"}
|
||||
result = lo.ChunkEntries(smallMap, 5)
|
||||
// iter.Seq[map[int]string] yielding:
|
||||
// map[int]string{1: "one", 2: "two"}
|
||||
|
||||
largeMap := make(map[int]bool)
|
||||
for i := 0; i < 10; i++ {
|
||||
largeMap[i] = true
|
||||
}
|
||||
result = lo.ChunkEntries(largeMap, 3)
|
||||
// iter.Seq[map[int]bool] yielding 4 maps with 3, 3, 3, and 1 entries respectively
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
name: ChunkString
|
||||
slug: chunkstring
|
||||
sourceRef: it/string.go#L130
|
||||
category: it
|
||||
subCategory: string
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#string#chunkstring
|
||||
similarHelpers:
|
||||
- core#string#chunkstring
|
||||
position: 0
|
||||
signatures:
|
||||
- "func ChunkString[T ~string](str T, size int) iter.Seq[T]"
|
||||
---
|
||||
|
||||
Returns a sequence of chunks of length `size` from the input string. If the string length is not a multiple of `size`, the final chunk contains the remaining characters. Panics if `size <= 0`.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Even split
|
||||
seq := it.ChunkString("123456", 2)
|
||||
var out []string
|
||||
for s := range seq { out = append(out, s) }
|
||||
// out == []string{"12", "34", "56"}
|
||||
```
|
||||
|
||||
```go
|
||||
// Remainder chunk
|
||||
seq := it.ChunkString("1234567", 2)
|
||||
var out []string
|
||||
for s := range seq { out = append(out, s) }
|
||||
// out == []string{"12", "34", "56", "7"}
|
||||
```
|
||||
|
||||
```go
|
||||
// Empty and small inputs
|
||||
seq1 := it.ChunkString("", 2)
|
||||
seq2 := it.ChunkString("1", 2)
|
||||
// seq1 yields ""
|
||||
// seq2 yields "1"
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
name: CoalesceSeq
|
||||
slug: coalesceseq
|
||||
sourceRef: it/type_manipulation.go#L65
|
||||
category: it
|
||||
subCategory: type
|
||||
signatures:
|
||||
- "func CoalesceSeq[T any](v ...iter.Seq[T]) (iter.Seq[T], bool)"
|
||||
variantHelpers:
|
||||
- it#type#coalesceseq
|
||||
similarHelpers:
|
||||
- it#type#coalesceseqorempty
|
||||
- core#type#coalesce
|
||||
- core#type#coalesceslice
|
||||
position: 100
|
||||
---
|
||||
|
||||
Returns the first non-empty sequence from the provided arguments, with a boolean indicating if a non-empty sequence was found.
|
||||
|
||||
```go
|
||||
emptySeq := func(yield func(int) bool) bool {
|
||||
return false // empty sequence
|
||||
}
|
||||
nonEmptySeq := lo.Range(3)
|
||||
result, ok := lo.CoalesceSeq(emptySeq, nonEmptySeq, emptySeq)
|
||||
// iter.Seq[int] yielding 0, 1, 2, true
|
||||
|
||||
emptyStrSeq := func(yield func(string) bool) bool {
|
||||
return false // empty sequence
|
||||
}
|
||||
strSeq := func(yield func(string) bool) bool {
|
||||
yield("a")
|
||||
yield("b")
|
||||
return true
|
||||
}
|
||||
result, ok = lo.CoalesceSeq(emptyStrSeq, strSeq)
|
||||
// iter.Seq[string] yielding "a", "b", true
|
||||
|
||||
result, ok = lo.CoalesceSeq(emptySeq, emptyStrSeq)
|
||||
// nil sequence, false
|
||||
```
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
name: CoalesceSeqOrEmpty
|
||||
slug: coalesceseqorempty
|
||||
sourceRef: it/type_manipulation.go#L76
|
||||
category: it
|
||||
subCategory: type
|
||||
signatures:
|
||||
- "func CoalesceSeqOrEmpty[T any](v ...iter.Seq[T]) iter.Seq[T]"
|
||||
variantHelpers:
|
||||
- it#type#coalesceseqorempty
|
||||
similarHelpers:
|
||||
- it#type#coalesceseq
|
||||
- core#type#coalesceorempty
|
||||
- core#type#coalescesliceorempty
|
||||
position: 102
|
||||
---
|
||||
|
||||
Returns the first non-empty sequence from the provided arguments, or an empty sequence if all arguments are empty.
|
||||
|
||||
```go
|
||||
emptySeq := func(yield func(int) bool) bool {
|
||||
return false // empty sequence
|
||||
}
|
||||
nonEmptySeq := lo.Range(3)
|
||||
result := lo.CoalesceSeqOrEmpty(emptySeq, nonEmptySeq, emptySeq)
|
||||
// iter.Seq[int] yielding 0, 1, 2
|
||||
|
||||
emptyStrSeq := func(yield func(string) bool) bool {
|
||||
return false // empty sequence
|
||||
}
|
||||
strSeq := func(yield func(string) bool) bool {
|
||||
yield("a")
|
||||
yield("b")
|
||||
return true
|
||||
}
|
||||
result = lo.CoalesceSeqOrEmpty(emptyStrSeq, strSeq)
|
||||
// iter.Seq[string] yielding "a", "b"
|
||||
|
||||
result = lo.CoalesceSeqOrEmpty(emptySeq, emptyStrSeq)
|
||||
// empty sequence (yields nothing)
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
name: Compact
|
||||
slug: compact
|
||||
sourceRef: it/seq.go#L699
|
||||
category: it
|
||||
subCategory: slice
|
||||
signatures:
|
||||
- "func Compact[T comparable, I ~func(func(T) bool)](collection I) I"
|
||||
variantHelpers:
|
||||
- it#slice#compact
|
||||
similarHelpers:
|
||||
- core#slice#compact
|
||||
position: 192
|
||||
---
|
||||
|
||||
Compact returns a sequence of all non-zero elements.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(0)
|
||||
yield(1)
|
||||
yield(0)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(0)
|
||||
}
|
||||
|
||||
compacted := it.Compact(collection)
|
||||
var result []int
|
||||
for item := range compacted {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [1, 2, 3]
|
||||
```
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
name: Contains
|
||||
slug: contains
|
||||
sourceRef: it/intersect.go#L13
|
||||
category: it
|
||||
subCategory: intersect
|
||||
signatures:
|
||||
- "func Contains[T comparable](collection iter.Seq[T], element T) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#intersect#contains
|
||||
similarHelpers:
|
||||
- core#slice#contains
|
||||
- it#intersect#containsby
|
||||
- it#intersect#some
|
||||
position: 0
|
||||
---
|
||||
|
||||
Returns true if an element is present in a collection.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(10)
|
||||
_ = yield(20)
|
||||
_ = yield(30)
|
||||
}
|
||||
has := it.Contains(seq, 20)
|
||||
// has == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("cherry")
|
||||
}
|
||||
has := it.Contains(seq, "orange")
|
||||
// has == false
|
||||
```
|
||||
@@ -0,0 +1,80 @@
|
||||
---
|
||||
name: ContainsBy
|
||||
slug: containsby
|
||||
sourceRef: it/intersect.go#L17
|
||||
category: it
|
||||
subCategory: intersect
|
||||
signatures:
|
||||
- "func ContainsBy[T any](collection iter.Seq[T], predicate func(item T) bool) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#intersect#containsby
|
||||
similarHelpers:
|
||||
- core#slice#containsby
|
||||
position: 650
|
||||
---
|
||||
|
||||
Returns true if predicate function returns true for any element in the collection.
|
||||
|
||||
Will iterate through the entire sequence if predicate never returns true.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Check if collection contains an even number
|
||||
numbers := it.Slice([]int{1, 3, 5, 7, 9})
|
||||
hasEven := it.ContainsBy(numbers, func(n int) bool { return n%2 == 0 })
|
||||
// hasEven: false
|
||||
|
||||
numbers = it.Slice([]int{1, 3, 5, 8, 9})
|
||||
hasEven = it.ContainsBy(numbers, func(n int) bool { return n%2 == 0 })
|
||||
// hasEven: true
|
||||
|
||||
// Check if collection contains a string with specific prefix
|
||||
words := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
hasPrefix := it.ContainsBy(words, func(s string) bool { return strings.HasPrefix(s, "go") })
|
||||
// hasPrefix: true
|
||||
|
||||
// Check if collection contains a person with specific age
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 35},
|
||||
})
|
||||
hasAge30 := it.ContainsBy(people, func(p Person) bool { return p.Age == 30 })
|
||||
// hasAge30: true
|
||||
|
||||
hasAge40 := it.ContainsBy(people, func(p Person) bool { return p.Age == 40 })
|
||||
// hasAge40: false
|
||||
|
||||
// Check if collection contains an element with specific property
|
||||
strings := it.Slice([]string{"apple", "banana", "cherry"})
|
||||
hasLongString := it.ContainsBy(strings, func(s string) bool { return len(s) > 5 })
|
||||
// hasLongString: true
|
||||
|
||||
// Check if collection contains negative numbers
|
||||
numbers = it.Slice([]int{1, -2, 3, 4, -5})
|
||||
hasNegative := it.ContainsBy(numbers, func(n int) bool { return n < 0 })
|
||||
// hasNegative: true
|
||||
|
||||
// Check if collection contains valid email
|
||||
emails := it.Slice([]string{"user@example.com", "invalid-email", "test@domain.org"})
|
||||
hasValidEmail := it.ContainsBy(emails, func(email string) bool {
|
||||
return strings.Contains(email, "@") && strings.Contains(email, ".")
|
||||
})
|
||||
// hasValidEmail: true
|
||||
|
||||
// Check empty collection
|
||||
empty := it.Slice([]int{})
|
||||
hasAny := it.ContainsBy(empty, func(n int) bool { return n > 0 })
|
||||
// hasAny: false
|
||||
|
||||
// Check for nil pointers (with pointer slice)
|
||||
ptrs := it.Slice([]*int{ptr(5), nil, ptr(10)})
|
||||
hasNil := it.ContainsBy(ptrs, func(p *int) bool { return p == nil })
|
||||
// hasNil: true
|
||||
```
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
name: Count / CountBy
|
||||
slug: count
|
||||
sourceRef: it/seq.go#L630
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Count[T comparable](collection iter.Seq[T], value T) int"
|
||||
- "func CountBy[T any](collection iter.Seq[T], predicate func(item T) bool) int"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#sequence#count
|
||||
- it#sequence#countby
|
||||
similarHelpers:
|
||||
- core#slice#count
|
||||
- it#sequence#countvalues
|
||||
position: 110
|
||||
---
|
||||
|
||||
Counts elements in a collection. Count counts elements equal to a value, CountBy counts elements matching a predicate.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(2)
|
||||
}
|
||||
cnt := it.Count(seq, 2)
|
||||
// cnt == 3
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("apricot")
|
||||
}
|
||||
cnt := it.CountBy(seq, func(s string) bool {
|
||||
return len(s) > 5
|
||||
})
|
||||
// cnt == 2 (banana, apricot)
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
name: CountValues
|
||||
slug: countvalues
|
||||
sourceRef: it/seq.go#L720
|
||||
category: it
|
||||
subCategory: slice
|
||||
signatures:
|
||||
- "func CountValues[T comparable](collection iter.Seq[T]) map[T]int"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#countvalues
|
||||
position: 203
|
||||
---
|
||||
|
||||
CountValues counts the number of each element in the collection.
|
||||
|
||||
```go
|
||||
collection := func(yield func(string) bool) {
|
||||
yield("apple")
|
||||
yield("banana")
|
||||
yield("apple")
|
||||
yield("cherry")
|
||||
yield("banana")
|
||||
yield("apple")
|
||||
}
|
||||
|
||||
counts := it.CountValues(collection)
|
||||
// counts contains {"apple": 3, "banana": 2, "cherry": 1}
|
||||
```
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
name: CountValuesBy
|
||||
slug: countvaluesby
|
||||
sourceRef: it/seq.go#L720
|
||||
category: it
|
||||
subCategory: slice
|
||||
signatures:
|
||||
- "func CountValuesBy[T any, U comparable](collection iter.Seq[T], transform func(item T) U) map[U]int"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#countvaluesby
|
||||
position: 204
|
||||
---
|
||||
|
||||
CountValuesBy counts the number of each element returned from transform function.
|
||||
Is equivalent to chaining Map and CountValues.
|
||||
|
||||
```go
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
collection := func(yield func(Person) bool) {
|
||||
yield(Person{"Alice", 25})
|
||||
yield(Person{"Bob", 30})
|
||||
yield(Person{"Charlie", 25})
|
||||
yield(Person{"Diana", 30})
|
||||
}
|
||||
|
||||
countsByAge := it.CountValuesBy(collection, func(p Person) int {
|
||||
return p.Age
|
||||
})
|
||||
// countsByAge contains {25: 2, 30: 2}
|
||||
```
|
||||
@@ -0,0 +1,54 @@
|
||||
---
|
||||
name: CrossJoinByX
|
||||
slug: crossjoinbyx
|
||||
sourceRef: it/tuples.go#L439
|
||||
category: it
|
||||
subCategory: tuple
|
||||
signatures:
|
||||
- "func CrossJoinBy2[T1, T2, R any](seq1 iter.Seq[T1], seq2 iter.Seq[T2], project func(T1, T2) R) iter.Seq[R]"
|
||||
- "func CrossJoinBy3[T1, T2, T3, R any](seq1 iter.Seq[T1], seq2 iter.Seq[T2], seq3 iter.Seq[T3], project func(T1, T2, T3) R) iter.Seq[R]"
|
||||
- "func CrossJoinBy4[T1, T2, T3, T4, R any](seq1 iter.Seq[T1], seq2 iter.Seq[T2], seq3 iter.Seq[T3], seq4 iter.Seq[T4], project func(T1, T2, T3, T4) R) iter.Seq[R]"
|
||||
- "func CrossJoinBy5[T1, T2, T3, T4, T5, R any](seq1 iter.Seq[T1], seq2 iter.Seq[T2], seq3 iter.Seq[T3], seq4 iter.Seq[T4], seq5 iter.Seq[T5], project func(T1, T2, T3, T4, T5) R) iter.Seq[R]"
|
||||
- "func CrossJoinBy6[T1, T2, T3, T4, T5, T6, R any](seq1 iter.Seq[T1], seq2 iter.Seq[T2], seq3 iter.Seq[T3], seq4 iter.Seq[T4], seq5 iter.Seq[T5], seq6 iter.Seq[T6], project func(T1, T2, T3, T4, T5, T6) R) iter.Seq[R]"
|
||||
- "func CrossJoinBy7[T1, T2, T3, T4, T5, T6, T7, R any](seq1 iter.Seq[T1], seq2 iter.Seq[T2], seq3 iter.Seq[T3], seq4 iter.Seq[T4], seq5 iter.Seq[T5], seq6 iter.Seq[T6], seq7 iter.Seq[T7], project func(T1, T2, T3, T4, T5, T6, T7) R) iter.Seq[R]"
|
||||
- "func CrossJoinBy8[T1, T2, T3, T4, T5, T6, T7, T8, R any](seq1 iter.Seq[T1], seq2 iter.Seq[T2], seq3 iter.Seq[T3], seq4 iter.Seq[T4], seq5 iter.Seq[T5], seq6 iter.Seq[T6], seq7 iter.Seq[T7], seq8 iter.Seq[T8], project func(T1, T2, T3, T4, T5, T6, T7, T8) R) iter.Seq[R]"
|
||||
- "func CrossJoinBy9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R any](seq1 iter.Seq[T1], seq2 iter.Seq[T2], seq3 iter.Seq[T3], seq4 iter.Seq[T4], seq5 iter.Seq[T5], seq6 iter.Seq[T6], seq7 iter.Seq[T7], seq8 iter.Seq[T8], seq9 iter.Seq[T9], project func(T1, T2, T3, T4, T5, T6, T7, T8, T9) R) iter.Seq[R]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#tuple#crossjoinby2
|
||||
- it#tuple#crossjoinby3
|
||||
- it#tuple#crossjoinby4
|
||||
- it#tuple#crossjoinby5
|
||||
- it#tuple#crossjoinby6
|
||||
- it#tuple#crossjoinby7
|
||||
- it#tuple#crossjoinby8
|
||||
- it#tuple#crossjoinby9
|
||||
similarHelpers:
|
||||
- core#tuple#crossjoinbyx
|
||||
- it#tuple#crossjoinx
|
||||
- core#slice#map
|
||||
position: 20
|
||||
---
|
||||
|
||||
Combines every item from multiple lists (cartesian product) using a project function to transform the results. Returns an empty sequence if any input sequence is empty.
|
||||
|
||||
Variants: `CrossJoinBy2..CrossJoinBy9`
|
||||
|
||||
```go
|
||||
seq1 := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
}
|
||||
seq2 := func(yield func(string) bool) {
|
||||
_ = yield("a")
|
||||
_ = yield("b")
|
||||
}
|
||||
result := it.CrossJoinBy2(seq1, seq2, func(i int, s string) string {
|
||||
return fmt.Sprintf("%d-%s", i, s)
|
||||
})
|
||||
var output []string
|
||||
for item := range result {
|
||||
output = append(output, item)
|
||||
}
|
||||
// output contains ["1-a", "1-b", "2-a", "2-b"]
|
||||
```
|
||||
@@ -0,0 +1,74 @@
|
||||
---
|
||||
name: CrossJoinX
|
||||
slug: crossjoinx
|
||||
sourceRef: it/tuples.go#L370
|
||||
category: it
|
||||
subCategory: tuple
|
||||
signatures:
|
||||
- "func CrossJoin2[T1, T2 any](list1 iter.Seq[T1], list2 iter.Seq[T2]) iter.Seq[lo.Tuple2[T1, T2]]"
|
||||
- "func CrossJoin3[T1, T2, T3 any](list1 iter.Seq[T1], list2 iter.Seq[T2], list3 iter.Seq[T3]) iter.Seq[lo.Tuple3[T1, T2, T3]]"
|
||||
- "func CrossJoin4[T1, T2, T3, T4 any](list1 iter.Seq[T1], list2 iter.Seq[T2], list3 iter.Seq[T3], list4 iter.Seq[T4]) iter.Seq[lo.Tuple4[T1, T2, T3, T4]]"
|
||||
- "func CrossJoin5[T1, T2, T3, T4, T5 any](list1 iter.Seq[T1], list2 iter.Seq[T2], list3 iter.Seq[T3], list4 iter.Seq[T4], list5 iter.Seq[T5]) iter.Seq[lo.Tuple5[T1, T2, T3, T4, T5]]"
|
||||
- "func CrossJoin6[T1, T2, T3, T4, T5, T6 any](list1 iter.Seq[T1], list2 iter.Seq[T2], list3 iter.Seq[T3], list4 iter.Seq[T4], list5 iter.Seq[T5], list6 iter.Seq[T6]) iter.Seq[lo.Tuple6[T1, T2, T3, T4, T5, T6]]"
|
||||
- "func CrossJoin7[T1, T2, T3, T4, T5, T6, T7 any](list1 iter.Seq[T1], list2 iter.Seq[T2], list3 iter.Seq[T3], list4 iter.Seq[T4], list5 iter.Seq[T5], list6 iter.Seq[T6], list7 iter.Seq[T7]) iter.Seq[lo.Tuple7[T1, T2, T3, T4, T5, T6, T7]]"
|
||||
- "func CrossJoin8[T1, T2, T3, T4, T5, T6, T7, T8 any](list1 iter.Seq[T1], list2 iter.Seq[T2], list3 iter.Seq[T3], list4 iter.Seq[T4], list5 iter.Seq[T5], list6 iter.Seq[T6], list7 iter.Seq[T7], list8 iter.Seq[T8]) iter.Seq[lo.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]]"
|
||||
- "func CrossJoin9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](list1 iter.Seq[T1], list2 iter.Seq[T2], list3 iter.Seq[T3], list4 iter.Seq[T4], list5 iter.Seq[T5], list6 iter.Seq[T6], list7 iter.Seq[T7], list8 iter.Seq[T8], list9 iter.Seq[T9]) iter.Seq[lo.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#tuple#crossjoin2
|
||||
- it#tuple#crossjoin3
|
||||
- it#tuple#crossjoin4
|
||||
- it#tuple#crossjoin5
|
||||
- it#tuple#crossjoin6
|
||||
- it#tuple#crossjoin7
|
||||
- it#tuple#crossjoin8
|
||||
- it#tuple#crossjoin9
|
||||
similarHelpers:
|
||||
- core#tuple#crossjoinx
|
||||
- it#tuple#zipx
|
||||
- it#tuple#crossjoinbyx
|
||||
position: 10
|
||||
---
|
||||
|
||||
Combines every item from multiple lists (cartesian product). The resulting sequence contains all possible combinations of elements from each input sequence.
|
||||
|
||||
The cartesian product means every element from the first sequence is paired with every element from the second sequence, then those pairs are combined with every element from the third sequence, and so on.
|
||||
|
||||
```go
|
||||
seq1 := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
}
|
||||
seq2 := func(yield func(string) bool) {
|
||||
_ = yield("a")
|
||||
_ = yield("b")
|
||||
}
|
||||
cross := it.CrossJoin2(seq1, seq2)
|
||||
var result []string
|
||||
for tuple := range cross {
|
||||
result = append(result, fmt.Sprintf("%d%s", tuple.A, tuple.B))
|
||||
}
|
||||
// result contains ["1a", "1b", "2a", "2b"] (all 4 combinations in lexical order)
|
||||
```
|
||||
|
||||
Example with 3 sequences:
|
||||
```go
|
||||
numbers := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
}
|
||||
letters := func(yield func(string) bool) {
|
||||
_ = yield("a")
|
||||
_ = yield("b")
|
||||
}
|
||||
colors := func(yield func(string) bool) {
|
||||
_ = yield("red")
|
||||
_ = yield("blue")
|
||||
}
|
||||
cross := it.CrossJoin3(numbers, letters, colors)
|
||||
var result []string
|
||||
for tuple := range cross {
|
||||
result = append(result, fmt.Sprintf("%d%s%s", tuple.A, tuple.B, tuple.C))
|
||||
}
|
||||
// result contains 8 combinations: ["1ared", "1ablue", "1bred", "1bblue", "2ared", "2ablue", "2bred", "2bblue"]
|
||||
```
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
name: CutPrefix
|
||||
slug: cutprefix
|
||||
sourceRef: it/seq.go#L778
|
||||
category: it
|
||||
subCategory: string
|
||||
signatures:
|
||||
- "func CutPrefix[T comparable, I ~func(func(T) bool)](collection I, separator []T) (after I, found bool)"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#string#cutprefix
|
||||
position: 260
|
||||
---
|
||||
|
||||
CutPrefix returns collection without the provided leading prefix and reports whether it found the prefix.
|
||||
If collection doesn't start with prefix, CutPrefix returns collection, false.
|
||||
If prefix is empty, CutPrefix returns collection, true.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
}
|
||||
|
||||
after, found := it.CutPrefix(collection, []int{1, 2})
|
||||
var result []int
|
||||
for item := range after {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [3, 4], found is true
|
||||
|
||||
after2, found2 := it.CutPrefix(collection, []int{9, 10})
|
||||
var result2 []int
|
||||
for item := range after2 {
|
||||
result2 = append(result2, item)
|
||||
}
|
||||
// result2 contains [1, 2, 3, 4], found2 is false
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
name: CutSuffix
|
||||
slug: cutsuffix
|
||||
sourceRef: it/seq.go#L778
|
||||
category: it
|
||||
subCategory: string
|
||||
signatures:
|
||||
- "func CutSuffix[T comparable, I ~func(func(T) bool)](collection I, separator []T) (before I, found bool)"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#string#cutsuffix
|
||||
position: 261
|
||||
---
|
||||
|
||||
CutSuffix returns collection without the provided ending suffix and reports whether it found the suffix.
|
||||
If collection doesn't end with suffix, CutSuffix returns collection, false.
|
||||
If suffix is empty, CutSuffix returns collection, true.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
}
|
||||
|
||||
before, found := it.CutSuffix(collection, []int{3, 4})
|
||||
var result []int
|
||||
for item := range before {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [1, 2], found is true
|
||||
```
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
name: Drain
|
||||
slug: drain
|
||||
sourceRef: it/seq.go#L26
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Drain[T any](collection iter.Seq[T])"
|
||||
variantHelpers: []
|
||||
similarHelpers: []
|
||||
position: 170
|
||||
---
|
||||
|
||||
Drain consumes an entire sequence.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
fmt.Println("yielding")
|
||||
}
|
||||
|
||||
it.Drain(collection)
|
||||
// prints "yielding" three times, sequence is consumed
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
name: Drop
|
||||
slug: drop
|
||||
sourceRef: it/seq.go#L498
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Drop[T any, I ~func(func(T) bool)](collection I, n int) I"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#sequence#drop
|
||||
similarHelpers:
|
||||
- core#slice#drop
|
||||
- it#sequence#droplast
|
||||
position: 100
|
||||
---
|
||||
|
||||
Drops n elements from the beginning of a sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
_ = yield(5)
|
||||
}
|
||||
dropped := it.Drop(seq, 2)
|
||||
var result []int
|
||||
for v := range dropped {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains 3, 4, 5
|
||||
```
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
name: DropLast
|
||||
slug: droplast
|
||||
sourceRef: it/seq.go#L512
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func DropLast[T any, I ~func(func(T) bool)](collection I, n int) I"
|
||||
variantHelpers:
|
||||
- it#sequence#droplast
|
||||
similarHelpers:
|
||||
- it#sequence#drop
|
||||
- it#sequence#dropwhile
|
||||
- it#sequence#droplastwhile
|
||||
- it#sequence#trim
|
||||
- it#sequence#trimsuffix
|
||||
position: 78
|
||||
---
|
||||
|
||||
Drops the last n elements from a sequence. Returns a new sequence without the specified number of trailing elements.
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
yield(5)
|
||||
}
|
||||
result := lo.DropLast(seq, 2)
|
||||
// iter.Seq[int] yielding 1, 2, 3
|
||||
|
||||
result = lo.DropLast(seq, 0)
|
||||
// iter.Seq[int] yielding 1, 2, 3, 4, 5 (unchanged)
|
||||
|
||||
result = lo.DropLast(seq, 10)
|
||||
// iter.Seq[int] yielding nothing (all elements dropped)
|
||||
|
||||
seq = func(yield func(string) bool) {
|
||||
yield("a")
|
||||
yield("b")
|
||||
yield("c")
|
||||
}
|
||||
result = lo.DropLast(seq, 1)
|
||||
// iter.Seq[string] yielding "a", "b"
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
name: DropLastWhile
|
||||
slug: droplastwhile
|
||||
sourceRef: it/seq.go#L180
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func DropLastWhile[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#droplastwhile
|
||||
position: 163
|
||||
---
|
||||
|
||||
DropLastWhile drops elements from the end of a sequence while the predicate returns true.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
yield(5)
|
||||
}
|
||||
|
||||
filtered := it.DropLastWhile(collection, func(x int) bool {
|
||||
return x > 3
|
||||
})
|
||||
var result []int
|
||||
for item := range filtered {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [1, 2, 3]
|
||||
```
|
||||
@@ -0,0 +1,101 @@
|
||||
---
|
||||
name: DropLast
|
||||
slug: dropslice
|
||||
sourceRef: it/seq.go#L510
|
||||
category: it
|
||||
subCategory: slice
|
||||
signatures:
|
||||
- "func DropLast[T any, I ~func(func(T) bool)](collection I, n int) I"
|
||||
- "func DropByIndex[T any, I ~func(func(T) bool)](collection I, indexes ...int) I"
|
||||
- "func Subset[T any, I ~func(func(T) bool)](collection I, offset, length int) I"
|
||||
- "func Slice[T any, I ~func(func(T) bool)](collection I, start, end int) I"
|
||||
variantHelpers:
|
||||
- it#slice#droplast
|
||||
- it#slice#dropbyindex
|
||||
- it#slice#subset
|
||||
- it#slice#slice
|
||||
similarHelpers:
|
||||
- core#slice#drop
|
||||
- core#slice#droplast
|
||||
- core#slice#dropbyindex
|
||||
- core#slice#subset
|
||||
- core#slice#slice
|
||||
position: 150
|
||||
---
|
||||
|
||||
DropLast drops n elements from the end of a sequence.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
yield(5)
|
||||
}
|
||||
|
||||
filtered := it.DropLast(collection, 2)
|
||||
var result []int
|
||||
for item := range filtered {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [1, 2, 3]
|
||||
```
|
||||
|
||||
DropByIndex drops elements from a sequence by the index.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
yield(5)
|
||||
}
|
||||
|
||||
filtered := it.DropByIndex(collection, 1, 3)
|
||||
var result []int
|
||||
for item := range filtered {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [1, 3, 5]
|
||||
```
|
||||
|
||||
Subset returns a subset of a sequence from `offset` up to `length` elements.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
yield(5)
|
||||
yield(6)
|
||||
}
|
||||
|
||||
subset := it.Subset(collection, 2, 3)
|
||||
var result []int
|
||||
for item := range subset {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [3, 4, 5]
|
||||
```
|
||||
|
||||
Slice returns a subset of a sequence from `start` up to, but not including `end`.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
yield(5)
|
||||
}
|
||||
|
||||
sliced := it.Slice(collection, 1, 4)
|
||||
var result []int
|
||||
for item := range sliced {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [2, 3, 4]
|
||||
```
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
name: DropWhile
|
||||
slug: dropwhile
|
||||
sourceRef: it/seq.go#L180
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func DropWhile[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#dropwhile
|
||||
- it#sequence#drop
|
||||
position: 162
|
||||
---
|
||||
|
||||
DropWhile drops elements from the beginning of a sequence while the predicate returns true.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
yield(5)
|
||||
}
|
||||
|
||||
filtered := it.DropWhile(collection, func(x int) bool {
|
||||
return x < 3
|
||||
})
|
||||
var result []int
|
||||
for item := range filtered {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [3, 4, 5]
|
||||
```
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
name: Earliest
|
||||
slug: earliest
|
||||
sourceRef: it/find.go#L278
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func Earliest(times iter.Seq[time.Time]) time.Time"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#earliest
|
||||
similarHelpers:
|
||||
- core#slice#earliest
|
||||
position: 500
|
||||
---
|
||||
|
||||
Searches for the earliest (minimum) time.Time in a collection.
|
||||
|
||||
Returns zero value when the collection is empty.
|
||||
Will iterate through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
import "time"
|
||||
|
||||
// Find the earliest time from a collection
|
||||
times := it.Slice([]time.Time{
|
||||
time.Date(2023, 5, 15, 10, 0, 0, 0, time.UTC),
|
||||
time.Date(2023, 3, 20, 14, 30, 0, 0, time.UTC),
|
||||
time.Date(2023, 8, 1, 9, 15, 0, 0, time.UTC),
|
||||
})
|
||||
earliest := it.Earliest(times)
|
||||
// earliest: 2023-03-20 14:30:00 +0000 UTC
|
||||
|
||||
// With empty collection
|
||||
empty := it.Slice([]time.Time{})
|
||||
earliest := it.Earliest(empty)
|
||||
// earliest: 0001-01-01 00:00:00 +0000 UTC (zero value)
|
||||
|
||||
// Find earliest from parsed times
|
||||
times := it.Slice([]time.Time{
|
||||
time.Parse(time.RFC3339, "2023-01-01T12:00:00Z"),
|
||||
time.Parse(time.RFC3339, "2023-01-01T10:00:00Z"),
|
||||
time.Parse(time.RFC3339, "2023-01-01T14:00:00Z"),
|
||||
})
|
||||
earliest := it.Earliest(times)
|
||||
// earliest: 2023-01-01 10:00:00 +0000 UTC
|
||||
```
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
name: EarliestBy
|
||||
slug: earliestby
|
||||
sourceRef: it/find.go#L285
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func EarliestBy[T any](collection iter.Seq[T], transform func(item T) time.Time) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#earliestby
|
||||
similarHelpers:
|
||||
- core#slice#earliestby
|
||||
position: 510
|
||||
---
|
||||
|
||||
Searches for the element with the earliest time using a transform function.
|
||||
|
||||
Returns zero value when the collection is empty.
|
||||
Will iterate through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
import "time"
|
||||
|
||||
type Event struct {
|
||||
Name string
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
// Find the earliest event by time
|
||||
events := it.Slice([]Event{
|
||||
{"Meeting", time.Date(2023, 5, 15, 10, 0, 0, 0, time.UTC)},
|
||||
{"Lunch", time.Date(2023, 5, 15, 12, 0, 0, 0, time.UTC)},
|
||||
{"Breakfast", time.Date(2023, 5, 15, 8, 0, 0, 0, time.UTC)},
|
||||
})
|
||||
earliest := it.EarliestBy(events, func(e Event) time.Time {
|
||||
return e.Time
|
||||
})
|
||||
// earliest: {Name: "Breakfast", Time: 2023-05-15 08:00:00 +0000 UTC}
|
||||
|
||||
// Find the earliest task by deadline
|
||||
type Task struct {
|
||||
ID int
|
||||
Deadline time.Time
|
||||
}
|
||||
tasks := it.Slice([]Task{
|
||||
{1, time.Date(2023, 6, 1, 0, 0, 0, 0, time.UTC)},
|
||||
{2, time.Date(2023, 5, 15, 0, 0, 0, 0, time.UTC)},
|
||||
{3, time.Date(2023, 7, 1, 0, 0, 0, 0, time.UTC)},
|
||||
})
|
||||
earliest := it.EarliestBy(tasks, func(t Task) time.Time {
|
||||
return t.Deadline
|
||||
})
|
||||
// earliest: {ID: 2, Deadline: 2023-05-15 00:00:00 +0000 UTC}
|
||||
```
|
||||
@@ -0,0 +1,127 @@
|
||||
---
|
||||
name: ElementsMatch
|
||||
slug: elementsmatch
|
||||
sourceRef: it/intersect.go#L174
|
||||
category: it
|
||||
subCategory: intersect
|
||||
signatures:
|
||||
- "func ElementsMatch[T comparable](list1, list2 iter.Seq[T]) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#intersect#elementsmatch
|
||||
similarHelpers:
|
||||
- core#slice#elementsmatch
|
||||
position: 720
|
||||
---
|
||||
|
||||
Returns true if lists contain the same set of elements (including empty set).
|
||||
|
||||
If there are duplicate elements, the number of occurrences in each list should match.
|
||||
The order of elements is not checked.
|
||||
Will iterate through each sequence before returning and allocate a map large enough to hold all distinct elements.
|
||||
Long heterogeneous input sequences can cause excessive memory usage.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Lists with same elements in different order
|
||||
list1 := it.Slice([]int{1, 2, 3, 4, 5})
|
||||
list2 := it.Slice([]int{5, 4, 3, 2, 1})
|
||||
match := it.ElementsMatch(list1, list2)
|
||||
// match: true
|
||||
|
||||
// Lists with different elements
|
||||
list1 = it.Slice([]int{1, 2, 3, 4, 5})
|
||||
list2 = it.Slice([]int{1, 2, 3, 4, 6})
|
||||
match = it.ElementsMatch(list1, list2)
|
||||
// match: false (5 vs 6)
|
||||
|
||||
// Lists with duplicates
|
||||
list1 = it.Slice([]int{1, 2, 2, 3, 4})
|
||||
list2 = it.Slice([]int{4, 3, 2, 1, 2})
|
||||
match = it.ElementsMatch(list1, list2)
|
||||
// match: true (both have two 2's)
|
||||
|
||||
// Lists with different number of duplicates
|
||||
list1 = it.Slice([]int{1, 2, 2, 3, 4})
|
||||
list2 = it.Slice([]int{4, 3, 2, 1, 1})
|
||||
match = it.ElementsMatch(list1, list2)
|
||||
// match: false (list1 has two 2's, list2 has two 1's)
|
||||
|
||||
// Empty lists
|
||||
empty1 := it.Slice([]int{})
|
||||
empty2 := it.Slice([]int{})
|
||||
match = it.ElementsMatch(empty1, empty2)
|
||||
// match: true
|
||||
|
||||
// One empty, one not empty
|
||||
empty := it.Slice([]int{})
|
||||
nonEmpty := it.Slice([]int{1, 2, 3})
|
||||
match = it.ElementsMatch(empty, nonEmpty)
|
||||
// match: false
|
||||
|
||||
// String lists
|
||||
words1 := it.Slice([]string{"hello", "world", "go"})
|
||||
words2 := it.Slice([]string{"go", "hello", "world"})
|
||||
match := it.ElementsMatch(words1, words2)
|
||||
// match: true
|
||||
|
||||
words1 = it.Slice([]string{"hello", "world", "go"})
|
||||
words2 = it.Slice([]string{"go", "hello", "golang"})
|
||||
match = it.ElementsMatch(words1, words2)
|
||||
// match: false
|
||||
|
||||
// Struct lists
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people1 := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 35},
|
||||
})
|
||||
people2 := it.Slice([]Person{
|
||||
{Name: "Charlie", Age: 35},
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
})
|
||||
match := it.ElementsMatch(people1, people2)
|
||||
// match: true
|
||||
|
||||
// Different lengths
|
||||
list1 = it.Slice([]int{1, 2, 3})
|
||||
list2 = it.Slice([]int{1, 2, 3, 4})
|
||||
match = it.ElementsMatch(list1, list2)
|
||||
// match: false
|
||||
|
||||
// Same elements but different counts
|
||||
list1 = it.Slice([]int{1, 1, 2, 3})
|
||||
list2 = it.Slice([]int{1, 2, 2, 3})
|
||||
match = it.ElementsMatch(list1, list2)
|
||||
// match: false (list1 has two 1's, list2 has two 2's)
|
||||
|
||||
// Boolean values
|
||||
bools1 := it.Slice([]bool{true, false, true})
|
||||
bools2 := it.Slice([]bool{true, true, false})
|
||||
match := it.ElementsMatch(bools1, bools2)
|
||||
// match: true
|
||||
|
||||
// Different boolean counts
|
||||
bools1 = it.Slice([]bool{true, false, true})
|
||||
bools2 = it.Slice([]bool{true, false, false})
|
||||
match = it.ElementsMatch(bools1, bools2)
|
||||
// match: false
|
||||
|
||||
// Lists with same single element
|
||||
list1 = it.Slice([]int{42})
|
||||
list2 = it.Slice([]int{42})
|
||||
match = it.ElementsMatch(list1, list2)
|
||||
// match: true
|
||||
|
||||
// Lists with different single elements
|
||||
list1 = it.Slice([]int{42})
|
||||
list2 = it.Slice([]int{43})
|
||||
match = it.ElementsMatch(list1, list2)
|
||||
// match: false
|
||||
```
|
||||
@@ -0,0 +1,177 @@
|
||||
---
|
||||
name: ElementsMatchBy
|
||||
slug: elementsmatchby
|
||||
sourceRef: it/intersect.go#L183
|
||||
category: it
|
||||
subCategory: intersect
|
||||
signatures:
|
||||
- "func ElementsMatchBy[T any, K comparable](list1, list2 iter.Seq[T], transform func(item T) K) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#intersect#elementsmatchby
|
||||
similarHelpers:
|
||||
- core#slice#elementsmatchby
|
||||
position: 730
|
||||
---
|
||||
|
||||
Returns true if lists contain the same set of elements' keys (including empty set).
|
||||
|
||||
If there are duplicate keys, the number of occurrences in each list should match.
|
||||
The order of elements is not checked.
|
||||
Will iterate through each sequence before returning and allocate a map large enough to hold all distinct transformed elements.
|
||||
Long heterogeneous input sequences can cause excessive memory usage.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Match people by age (ignoring names)
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people1 := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 35},
|
||||
})
|
||||
people2 := it.Slice([]Person{
|
||||
{Name: "David", Age: 35},
|
||||
{Name: "Eve", Age: 25},
|
||||
{Name: "Frank", Age: 30},
|
||||
})
|
||||
match := it.ElementsMatchBy(people1, people2, func(p Person) int { return p.Age })
|
||||
// match: true (both have ages 25, 30, 35)
|
||||
|
||||
// Match by string length
|
||||
words1 := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
words2 := it.Slice([]string{"short", "longer", "golang", "python"})
|
||||
match = it.ElementsMatchBy(words1, words2, func(s string) int { return len(s) })
|
||||
// match: false (lengths: 5,5,2,4 vs 6,6,6,6)
|
||||
|
||||
// Match by first character
|
||||
items1 := it.Slice([]string{"apple", "apricot", "banana", "blueberry"})
|
||||
items2 := it.Slice([]string{"ant", "anchor", "boat", "berry"})
|
||||
match = it.ElementsMatchBy(items1, items2, func(s string) byte { return s[0] })
|
||||
// match: true (both start with a, a, b, b)
|
||||
|
||||
// Match emails by domain
|
||||
type Email struct {
|
||||
Address string
|
||||
}
|
||||
emails1 := it.Slice([]Email{
|
||||
{Address: "user1@example.com"},
|
||||
{Address: "user2@gmail.com"},
|
||||
{Address: "user3@example.com"},
|
||||
})
|
||||
emails2 := it.Slice([]Email{
|
||||
{Address: "different@gmail.com"},
|
||||
{Address: "another@example.com"},
|
||||
{Address: "third@example.com"},
|
||||
})
|
||||
match = it.ElementsMatchBy(emails1, emails2, func(e Email) string {
|
||||
parts := strings.Split(e.Address, "@")
|
||||
if len(parts) > 1 {
|
||||
return parts[1]
|
||||
}
|
||||
return ""
|
||||
})
|
||||
// match: true (both have domains: example.com, gmail.com, example.com)
|
||||
|
||||
// Match by modulo operation
|
||||
numbers1 := it.Slice([]int{1, 2, 3, 4, 5, 6})
|
||||
numbers2 := it.Slice([]int{7, 8, 9, 10, 11, 12})
|
||||
match = it.ElementsMatchBy(numbers1, numbers2, func(n int) int { return n % 3 })
|
||||
// match: true (remainders: 1,2,0,1,2,0 vs 1,2,0,1,2,0)
|
||||
|
||||
// Match by case-insensitive strings
|
||||
strings1 := it.Slice([]string{"Hello", "World", "GO"})
|
||||
strings2 := it.Slice([]string{"hello", "world", "go"})
|
||||
match = it.ElementsMatchBy(strings1, strings2, func(s string) string { return strings.ToLower(s) })
|
||||
// match: true
|
||||
|
||||
// Match orders by customer ID (ignoring order details)
|
||||
type Order struct {
|
||||
ID string
|
||||
CustomerID string
|
||||
ProductID string
|
||||
}
|
||||
orders1 := it.Slice([]Order{
|
||||
{ID: "1", CustomerID: "A", ProductID: "X"},
|
||||
{ID: "2", CustomerID: "B", ProductID: "Y"},
|
||||
{ID: "3", CustomerID: "A", ProductID: "Z"},
|
||||
})
|
||||
orders2 := it.Slice([]Order{
|
||||
{ID: "4", CustomerID: "B", ProductID: "W"},
|
||||
{ID: "5", CustomerID: "A", ProductID: "V"},
|
||||
{ID: "6", CustomerID: "A", ProductID: "U"},
|
||||
})
|
||||
match = it.ElementsMatchBy(orders1, orders2, func(o Order) string { return o.CustomerID })
|
||||
// match: true (both have customer IDs: A, B, A)
|
||||
|
||||
// Match dates by year-month (ignoring day)
|
||||
import "time"
|
||||
dates1 := it.Slice([]time.Time{
|
||||
time.Date(2023, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
time.Date(2023, 2, 20, 0, 0, 0, 0, time.UTC),
|
||||
time.Date(2023, 1, 25, 0, 0, 0, 0, time.UTC),
|
||||
})
|
||||
dates2 := it.Slice([]time.Time{
|
||||
time.Date(2023, 1, 5, 0, 0, 0, 0, time.UTC),
|
||||
time.Date(2023, 2, 10, 0, 0, 0, 0, time.UTC),
|
||||
time.Date(2023, 1, 30, 0, 0, 0, 0, time.UTC),
|
||||
})
|
||||
match = it.ElementsMatchBy(dates1, dates2, func(t time.Time) string {
|
||||
return fmt.Sprintf("%d-%02d", t.Year(), t.Month())
|
||||
})
|
||||
// match: true (both have: 2023-01, 2023-02, 2023-01)
|
||||
|
||||
// Match by category function
|
||||
type Product struct {
|
||||
Name string
|
||||
Price float64
|
||||
}
|
||||
products1 := it.Slice([]Product{
|
||||
{Name: "Book", Price: 19.99},
|
||||
{Name: "Pen", Price: 1.99},
|
||||
{Name: "Laptop", Price: 999.99},
|
||||
})
|
||||
products2 := it.Slice([]Product{
|
||||
{Name: "Pencil", Price: 0.99},
|
||||
{Name: "Phone", Price: 699.99},
|
||||
{Name: "Desk", Price: 199.99},
|
||||
})
|
||||
match = it.ElementsMatchBy(products1, products2, func(p Product) string {
|
||||
if p.Price < 10 {
|
||||
return "cheap"
|
||||
} else if p.Price < 100 {
|
||||
return "medium"
|
||||
}
|
||||
return "expensive"
|
||||
})
|
||||
// match: true (both have: medium, cheap, expensive)
|
||||
|
||||
// Match by custom classification
|
||||
type Student struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
students1 := it.Slice([]Student{
|
||||
{Name: "Alice", Age: 8},
|
||||
{Name: "Bob", Age: 15},
|
||||
{Name: "Charlie", Age: 20},
|
||||
})
|
||||
students2 := it.Slice([]Student{
|
||||
{Name: "Diana", Age: 25},
|
||||
{Name: "Eve", Age: 10},
|
||||
{Name: "Frank", Age: 12},
|
||||
})
|
||||
match = it.ElementsMatchBy(students1, students2, func(s Student) string {
|
||||
if s.Age < 12 {
|
||||
return "child"
|
||||
} else if s.Age < 18 {
|
||||
return "teen"
|
||||
}
|
||||
return "adult"
|
||||
})
|
||||
// match: false (students1: child, teen, adult vs students2: adult, child, child)
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: Empty
|
||||
slug: empty
|
||||
sourceRef: it/type_manipulation.go#L44
|
||||
category: it
|
||||
subCategory: type
|
||||
signatures:
|
||||
- "func Empty[T any]() iter.Seq[T]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#type#empty
|
||||
similarHelpers:
|
||||
- it#type#isempty
|
||||
- it#type#isnotempty
|
||||
position: 0
|
||||
---
|
||||
|
||||
Returns an empty sequence of the specified type.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
emptySeq := it.Empty[int]()
|
||||
count := 0
|
||||
for range emptySeq {
|
||||
count++
|
||||
}
|
||||
// count == 0
|
||||
```
|
||||
|
||||
```go
|
||||
emptySeq := it.Empty[string]()
|
||||
var result []string
|
||||
for v := range emptySeq {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result is empty slice
|
||||
```
|
||||
@@ -0,0 +1,37 @@
|
||||
---
|
||||
name: Entries
|
||||
slug: entries
|
||||
sourceRef: it/map.go#L77
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func Entries[K comparable, V any](in ...map[K]V) iter.Seq2[K, V]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#map#entries
|
||||
similarHelpers:
|
||||
- core#slice#entries
|
||||
- it#map#fromentries
|
||||
- it#map#topairs
|
||||
position: 20
|
||||
---
|
||||
|
||||
Transforms a map into a sequence of key/value pairs. Accepts multiple maps and concatenates their entries.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
m := map[string]int{
|
||||
"apple": 1,
|
||||
"banana": 2,
|
||||
"cherry": 3,
|
||||
}
|
||||
entriesSeq := it.Entries(m)
|
||||
var keys []string
|
||||
var values []int
|
||||
for k, v := range entriesSeq {
|
||||
keys = append(keys, k)
|
||||
values = append(values, v)
|
||||
}
|
||||
// keys contains map keys, values contains corresponding values
|
||||
```
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
name: Every
|
||||
slug: every
|
||||
sourceRef: it/intersect.go#L25
|
||||
category: it
|
||||
subCategory: intersect
|
||||
signatures:
|
||||
- "func Every[T comparable](collection iter.Seq[T], subset ...T) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#intersect#every
|
||||
similarHelpers:
|
||||
- core#slice#every
|
||||
- it#intersect#some
|
||||
- it#intersect#none
|
||||
position: 30
|
||||
---
|
||||
|
||||
Returns true if all elements of a subset are contained in a collection.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
_ = yield(5)
|
||||
}
|
||||
hasAll := it.Every(seq, 2, 4)
|
||||
// hasAll == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("cherry")
|
||||
}
|
||||
hasAll := it.Every(seq, "apple", "orange")
|
||||
// hasAll == false (orange is not in collection)
|
||||
```
|
||||
@@ -0,0 +1,97 @@
|
||||
---
|
||||
name: EveryBy
|
||||
slug: everyby
|
||||
sourceRef: it/intersect.go#L43
|
||||
category: it
|
||||
subCategory: intersect
|
||||
signatures:
|
||||
- "func EveryBy[T any](collection iter.Seq[T], predicate func(item T) bool) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#intersect#everyby
|
||||
similarHelpers:
|
||||
- core#slice#everyby
|
||||
position: 660
|
||||
---
|
||||
|
||||
Returns true if the predicate returns true for all elements in the collection or if the collection is empty.
|
||||
|
||||
Will iterate through the entire sequence if predicate never returns false.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Check if all numbers are positive
|
||||
numbers := it.Slice([]int{1, 3, 5, 7, 9})
|
||||
allPositive := it.EveryBy(numbers, func(n int) bool { return n > 0 })
|
||||
// allPositive: true
|
||||
|
||||
numbers = it.Slice([]int{1, -3, 5, 7, 9})
|
||||
allPositive = it.EveryBy(numbers, func(n int) bool { return n > 0 })
|
||||
// allPositive: false
|
||||
|
||||
// Check if all strings have minimum length
|
||||
words := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
allLongEnough := it.EveryBy(words, func(s string) bool { return len(s) >= 2 })
|
||||
// allLongEnough: true
|
||||
|
||||
allVeryLong := it.EveryBy(words, func(s string) bool { return len(s) >= 5 })
|
||||
// allVeryLong: false
|
||||
|
||||
// Check if all people are adults
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 35},
|
||||
})
|
||||
allAdults := it.EveryBy(people, func(p Person) bool { return p.Age >= 18 })
|
||||
// allAdults: true
|
||||
|
||||
minors := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 15}, // Not adult
|
||||
{Name: "Charlie", Age: 35},
|
||||
})
|
||||
allAdults = it.EveryBy(minors, func(p Person) bool { return p.Age >= 18 })
|
||||
// allAdults: false
|
||||
|
||||
// Check if all numbers are even
|
||||
numbers = it.Slice([]int{2, 4, 6, 8, 10})
|
||||
allEven := it.EveryBy(numbers, func(n int) bool { return n%2 == 0 })
|
||||
// allEven: true
|
||||
|
||||
numbers = it.Slice([]int{2, 4, 6, 7, 10}) // 7 is odd
|
||||
allEven = it.EveryBy(numbers, func(n int) bool { return n%2 == 0 })
|
||||
// allEven: false
|
||||
|
||||
// Check if all strings are lowercase
|
||||
strings := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
allLowercase := it.EveryBy(strings, func(s string) bool { return s == strings.ToLower(s) })
|
||||
// allLowercase: true
|
||||
|
||||
strings = it.Slice([]string{"hello", "World", "go", "lang"}) // "World" has uppercase
|
||||
allLowercase = it.EveryBy(strings, func(s string) bool { return s == strings.ToLower(s) })
|
||||
// allLowercase: false
|
||||
|
||||
// Empty collection returns true
|
||||
empty := it.Slice([]int{})
|
||||
allPositive := it.EveryBy(empty, func(n int) bool { return n > 0 })
|
||||
// allPositive: true
|
||||
|
||||
// Check if all emails are valid
|
||||
emails := it.Slice([]string{"user@example.com", "test@domain.org", "admin@site.net"})
|
||||
allValid := it.EveryBy(emails, func(email string) bool {
|
||||
return strings.Contains(email, "@") && strings.Contains(email, ".")
|
||||
})
|
||||
// allValid: true
|
||||
|
||||
emails = it.Slice([]string{"user@example.com", "invalid-email", "test@domain.org"})
|
||||
allValid = it.EveryBy(emails, func(email string) bool {
|
||||
return strings.Contains(email, "@") && strings.Contains(email, ".")
|
||||
})
|
||||
// allValid: false
|
||||
```
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
name: Fill
|
||||
slug: fill
|
||||
sourceRef: it/seq.go#L26
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Fill[T lo.Clonable[T], I ~func(func(T) bool)](collection I, initial T) I"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#fill
|
||||
position: 174
|
||||
---
|
||||
|
||||
Fill replaces elements of a sequence with `initial` value.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
}
|
||||
|
||||
filled := it.Fill(collection, 99)
|
||||
var result []int
|
||||
for item := range filled {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [99, 99, 99]
|
||||
```
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
name: Filter
|
||||
slug: filter
|
||||
sourceRef: it/seq.go#L33
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Filter[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#sequence#filter
|
||||
- it#sequence#filteri
|
||||
similarHelpers:
|
||||
- core#slice#filter
|
||||
- it#sequence#reject
|
||||
position: 10
|
||||
---
|
||||
|
||||
Returns a sequence of all elements for which the predicate function returns true.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
_ = yield(5)
|
||||
}
|
||||
filtered := it.Filter(seq, func(x int) bool {
|
||||
return x%2 == 0
|
||||
})
|
||||
var result []int
|
||||
for v := range filtered {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains 2, 4 (even numbers)
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
name: FilterKeys
|
||||
slug: filterkeys
|
||||
sourceRef: it/map.go#L238
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func FilterKeys[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) []K"
|
||||
variantHelpers:
|
||||
- it#map#filterkeys
|
||||
similarHelpers:
|
||||
- core#map#filterkeys
|
||||
- it#map#filtervalues
|
||||
- it#map#keys
|
||||
- it#map#values
|
||||
position: 56
|
||||
---
|
||||
|
||||
Filters map keys based on a predicate function that takes both key and value. Returns a slice of keys that satisfy the predicate.
|
||||
|
||||
```go
|
||||
m := map[string]int{
|
||||
"apple": 3,
|
||||
"banana": 5,
|
||||
"cherry": 2,
|
||||
"date": 0,
|
||||
}
|
||||
result := lo.FilterKeys(m, func(key string, value int) bool {
|
||||
return value > 2
|
||||
})
|
||||
// []string{"apple", "banana"} (keys with values > 2)
|
||||
|
||||
numberMap := map[int]string{1: "one", 2: "two", 3: "three", 4: "four"}
|
||||
result = lo.FilterKeys(numberMap, func(key int, value string) bool {
|
||||
return len(value) == 3
|
||||
})
|
||||
// []int{1, 2, 3} (keys where value length is 3)
|
||||
|
||||
personMap := map[string]int{"alice": 25, "bob": 30, "charlie": 17}
|
||||
result = lo.FilterKeys(personMap, func(key string, age int) bool {
|
||||
return strings.HasPrefix(key, "a") && age >= 20
|
||||
})
|
||||
// []string{"alice"} (keys starting with "a" and age >= 20)
|
||||
|
||||
emptyMap := map[string]int{}
|
||||
result = lo.FilterKeys(emptyMap, func(key string, value int) bool {
|
||||
return true
|
||||
})
|
||||
// []string{} (empty map)
|
||||
```
|
||||
@@ -0,0 +1,70 @@
|
||||
---
|
||||
name: FilterMap
|
||||
slug: filtermap
|
||||
sourceRef: it/seq.go#L86
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func FilterMap[T, R any](collection iter.Seq[T], callback func(item T) (R, bool)) iter.Seq[R]"
|
||||
- "func FilterMapI[T, R any](collection iter.Seq[T], callback func(item T, index int) (R, bool)) iter.Seq[R]"
|
||||
variantHelpers:
|
||||
- it#sequence#filtermap
|
||||
- it#sequence#filtermapi
|
||||
similarHelpers:
|
||||
- it#sequence#map
|
||||
- it#sequence#filter
|
||||
- it#sequence#filtermaptoslice
|
||||
- it#sequence#rejectmap
|
||||
position: 40
|
||||
---
|
||||
|
||||
Maps elements of a sequence to new values and filters out elements where the callback returns false. Only elements where the second return value is true are included in the result.
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
}
|
||||
result := lo.FilterMap(seq, func(x int) (string, bool) {
|
||||
if x%2 == 0 {
|
||||
return fmt.Sprintf("even-%d", x), true
|
||||
}
|
||||
return "", false
|
||||
})
|
||||
// iter.Seq[string] yielding "even-2", "even-4"
|
||||
|
||||
seq = func(yield func(string) bool) {
|
||||
yield("a")
|
||||
yield("")
|
||||
yield("c")
|
||||
yield("d")
|
||||
}
|
||||
result = lo.FilterMap(seq, func(s string) (int, bool) {
|
||||
if s != "" {
|
||||
return len(s), true
|
||||
}
|
||||
return 0, false
|
||||
})
|
||||
// iter.Seq[int] yielding 1, 1, 1 (length of "a", "c", "d")
|
||||
```
|
||||
|
||||
### FilterMapI
|
||||
|
||||
Maps elements of a sequence to new values and filters out elements where the callback returns false. The callback receives both the item and its index.
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
yield("apple")
|
||||
yield("banana")
|
||||
yield("cherry")
|
||||
}
|
||||
result := lo.FilterMapI(seq, func(s string, index int) (string, bool) {
|
||||
if index%2 == 0 {
|
||||
return fmt.Sprintf("%s-%d", s, index), true
|
||||
}
|
||||
return "", false
|
||||
})
|
||||
// iter.Seq[string] yielding "apple-0", "cherry-2"
|
||||
```
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
name: FilterMapToSeq
|
||||
slug: filtermaptoseq
|
||||
sourceRef: it/map.go#L178
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func FilterMapToSeq[K comparable, V, R any](in map[K]V, transform func(key K, value V) (R, bool)) iter.Seq[R]"
|
||||
variantHelpers:
|
||||
- it#map#filtermaptoseq
|
||||
similarHelpers:
|
||||
- it#map#maptoseq
|
||||
- core#map#filtermaptoslice
|
||||
- it#map#filterkeys
|
||||
- it#map#filtervalues
|
||||
position: 54
|
||||
---
|
||||
|
||||
Transforms a map into a sequence by applying a transform function to each key-value pair, but only includes values where the transform function returns true as the second value.
|
||||
|
||||
```go
|
||||
m := map[string]int{
|
||||
"apple": 3,
|
||||
"banana": 0,
|
||||
"cherry": 2,
|
||||
"date": 0,
|
||||
}
|
||||
result := lo.FilterMapToSeq(m, func(key string, value int) (string, bool) {
|
||||
if value > 0 {
|
||||
return fmt.Sprintf("%s:%d", key, value), true
|
||||
}
|
||||
return "", false
|
||||
})
|
||||
// iter.Seq[string] yielding "apple:3", "cherry:2" (only entries with value > 0)
|
||||
|
||||
personMap := map[string]int{"alice": 25, "bob": 30, "charlie": 15}
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
result = lo.FilterMapToSeq(personMap, func(name string, age int) (Person, bool) {
|
||||
person := Person{Name: name, Age: age}
|
||||
return person, age >= 18
|
||||
})
|
||||
// iter.Seq[Person] yielding {Name: "alice", Age: 25}, {Name: "bob", Age: 30} (only adults)
|
||||
|
||||
dataMap := map[string]float64{"a": 1.5, "b": -2.0, "c": 3.14}
|
||||
result = lo.FilterMapToSeq(dataMap, func(key string, value float64) (int, bool) {
|
||||
if value > 0 {
|
||||
return int(value * 100), true
|
||||
}
|
||||
return 0, false
|
||||
})
|
||||
// iter.Seq[int] yielding 150, 314 (1.5*100, 3.14*100 rounded)
|
||||
```
|
||||
@@ -0,0 +1,56 @@
|
||||
---
|
||||
name: FilterValues
|
||||
slug: filtervalues
|
||||
sourceRef: it/map.go#L253
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func FilterValues[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) []V"
|
||||
variantHelpers:
|
||||
- it#map#filtervalues
|
||||
similarHelpers:
|
||||
- core#map#filtervalues
|
||||
- it#map#filterkeys
|
||||
- it#map#keys
|
||||
- it#map#values
|
||||
position: 58
|
||||
---
|
||||
|
||||
Filters map values based on a predicate function that takes both key and value. Returns a slice of values that satisfy the predicate.
|
||||
|
||||
```go
|
||||
m := map[string]int{
|
||||
"apple": 3,
|
||||
"banana": 5,
|
||||
"cherry": 2,
|
||||
"date": 0,
|
||||
}
|
||||
result := lo.FilterValues(m, func(key string, value int) bool {
|
||||
return value > 2
|
||||
})
|
||||
// []int{3, 5} (values > 2, corresponds to "apple" and "banana")
|
||||
|
||||
numberMap := map[int]string{1: "one", 2: "two", 3: "three", 4: "four"}
|
||||
result = lo.FilterValues(numberMap, func(key int, value string) bool {
|
||||
return len(value) == 3
|
||||
})
|
||||
// []string{"one", "two", "three"} (values with length 3)
|
||||
|
||||
personMap := map[string]int{"alice": 25, "bob": 30, "charlie": 17}
|
||||
result = lo.FilterValues(personMap, func(key string, age int) bool {
|
||||
return strings.HasPrefix(key, "a") && age >= 20
|
||||
})
|
||||
// []int{25} (value for "alice" only)
|
||||
|
||||
emptyMap := map[string]int{}
|
||||
result = lo.FilterValues(emptyMap, func(key string, value int) bool {
|
||||
return true
|
||||
})
|
||||
// []int{} (empty map)
|
||||
|
||||
dataMap := map[string]float64{"a": 1.5, "b": -2.0, "c": 0.0, "d": 3.14}
|
||||
result = lo.FilterValues(dataMap, func(key string, value float64) bool {
|
||||
return value > 0
|
||||
})
|
||||
// []float64{1.5, 3.14} (positive values only)
|
||||
```
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
name: Find
|
||||
slug: find
|
||||
sourceRef: it/find.go#L105
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func Find[T any](collection iter.Seq[T], predicate func(item T) bool) (T, bool)"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#find
|
||||
similarHelpers:
|
||||
- core#slice#find
|
||||
- it#find#findindexof
|
||||
- it#find#findorelse
|
||||
position: 40
|
||||
---
|
||||
|
||||
Searches for an element in a sequence based on a predicate function. Returns the element and true if found, zero value and false if not found.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(10)
|
||||
_ = yield(20)
|
||||
_ = yield(30)
|
||||
_ = yield(40)
|
||||
}
|
||||
found, ok := it.Find(seq, func(x int) bool {
|
||||
return x > 25
|
||||
})
|
||||
// found == 30, ok == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("cherry")
|
||||
}
|
||||
found, ok := it.Find(seq, func(s string) bool {
|
||||
return len(s) > 10
|
||||
})
|
||||
// found == "", ok == false (no element longer than 10 chars)
|
||||
```
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
name: FindDuplicates
|
||||
slug: findduplicates
|
||||
sourceRef: it/find.go#L196
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func FindDuplicates[T comparable, I ~func(func(T) bool)](collection I) I"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#findduplicates
|
||||
similarHelpers:
|
||||
- core#slice#findduplicates
|
||||
- it#find#finduniques
|
||||
- it#find#findduplicatesby
|
||||
position: 90
|
||||
---
|
||||
|
||||
Returns the first occurrence of each duplicated element in the collection (elements that appear more than once).
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
_ = yield(4)
|
||||
_ = yield(4)
|
||||
}
|
||||
dupSeq := it.FindDuplicates(seq)
|
||||
var result []int
|
||||
for v := range dupSeq {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains 2, 4 (first occurrence of each duplicated element)
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("apple")
|
||||
_ = yield("cherry")
|
||||
_ = yield("banana")
|
||||
}
|
||||
dupSeq := it.FindDuplicates(seq)
|
||||
var result []string
|
||||
for v := range dupSeq {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains "apple", "banana" (duplicated elements)
|
||||
```
|
||||
@@ -0,0 +1,113 @@
|
||||
---
|
||||
name: FindDuplicatesBy
|
||||
slug: findduplicatesby
|
||||
sourceRef: it/find.go#L200
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func FindDuplicatesBy[T any, U comparable, I ~func(func(T) bool)](collection I, transform func(item T) U) I"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#findduplicatesby
|
||||
similarHelpers:
|
||||
- core#slice#findduplicatesby
|
||||
position: 640
|
||||
---
|
||||
|
||||
Returns a sequence with the first occurrence of each duplicated element in the collection, based on a transform function.
|
||||
|
||||
The order of result values is determined by the order duplicates occur in the sequence. A transform function is
|
||||
invoked for each element in the sequence to generate the criterion by which uniqueness is computed.
|
||||
Will allocate a map large enough to hold all distinct transformed elements.
|
||||
Long heterogeneous input sequences can cause excessive memory usage.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Find duplicate people by age
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 30}, // Same age as Alice - Alice is returned
|
||||
{Name: "Diana", Age: 30}, // Same age as Alice - already marked as duplicate
|
||||
{Name: "Eve", Age: 25}, // Same age as Bob - Bob is returned
|
||||
})
|
||||
duplicates := it.FindDuplicatesBy(people, func(p Person) int { return p.Age })
|
||||
// duplicates: sequence with Alice (age 30) and Bob (age 25)
|
||||
|
||||
// Find duplicate strings by length
|
||||
words := it.Slice([]string{"hello", "world", "hi", "go", "bye", "yes"})
|
||||
duplicates := it.FindDuplicatesBy(words, func(s string) int { return len(s) })
|
||||
// duplicates: sequence with "hello" (length 5, also "world" has length 5)
|
||||
// and "hi" (length 2, also "go", "yes" have length 2)
|
||||
|
||||
// Find duplicate items by first letter
|
||||
items := it.Slice([]string{"apple", "apricot", "banana", "blueberry", "cherry", "cranberry"})
|
||||
duplicates := it.FindDuplicatesBy(items, func(s string) byte { return s[0] })
|
||||
// duplicates: sequence with "apple" (starts with 'a', also "apricot"),
|
||||
// "banana" (starts with 'b', also "blueberry"),
|
||||
// "cherry" (starts with 'c', also "cranberry")
|
||||
|
||||
// Find duplicate numbers by modulo
|
||||
numbers := it.Slice([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})
|
||||
duplicates := it.FindDuplicatesBy(numbers, func(n int) int { return n % 3 })
|
||||
// duplicates: sequence with 1, 2, 3 (remainders 1, 2, 0 appear multiple times)
|
||||
|
||||
// Find duplicate structs by composite key
|
||||
type Order struct {
|
||||
CustomerID string
|
||||
ProductID string
|
||||
}
|
||||
orders := it.Slice([]Order{
|
||||
{CustomerID: "A", ProductID: "1"},
|
||||
{CustomerID: "A", ProductID: "2"},
|
||||
{CustomerID: "B", ProductID: "1"}, // Same customer as first
|
||||
{CustomerID: "C", ProductID: "3"},
|
||||
{CustomerID: "A", ProductID: "3"}, // Same customer as first two
|
||||
})
|
||||
duplicates := it.FindDuplicatesBy(orders, func(o Order) string { return o.CustomerID })
|
||||
// duplicates: sequence with first order {CustomerID: "A", ProductID: "1"}
|
||||
|
||||
// Find duplicate items by case-insensitive comparison
|
||||
words := it.Slice([]string{"Hello", "hello", "WORLD", "world", "Go", "GO", "go"})
|
||||
duplicates := it.FindDuplicatesBy(words, func(s string) string { return strings.ToLower(s) })
|
||||
// duplicates: sequence with "Hello", "WORLD", "Go" (first occurrences of each case-insensitive duplicate)
|
||||
|
||||
// Find duplicate dates by year-month
|
||||
import "time"
|
||||
dates := it.Slice([]time.Time{
|
||||
time.Date(2023, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
time.Date(2023, 1, 20, 0, 0, 0, 0, time.UTC), // Same month as first
|
||||
time.Date(2023, 2, 10, 0, 0, 0, 0, time.UTC),
|
||||
time.Date(2022, 1, 5, 0, 0, 0, 0, time.UTC), // Different year
|
||||
time.Date(2023, 2, 25, 0, 0, 0, 0, time.UTC), // Same month as third
|
||||
})
|
||||
duplicates := it.FindDuplicatesBy(dates, func(t time.Time) string {
|
||||
return fmt.Sprintf("%d-%02d", t.Year(), t.Month())
|
||||
})
|
||||
// duplicates: sequence with first January date and first February date
|
||||
|
||||
// Find duplicate emails by domain
|
||||
type Email struct {
|
||||
Address string
|
||||
}
|
||||
emails := it.Slice([]Email{
|
||||
{Address: "user1@example.com"},
|
||||
{Address: "user2@example.com"}, // Same domain as first
|
||||
{Address: "user3@gmail.com"},
|
||||
{Address: "user4@example.com"}, // Same domain as first two
|
||||
{Address: "user5@yahoo.com"},
|
||||
})
|
||||
duplicates := it.FindDuplicatesBy(emails, func(e Email) string {
|
||||
parts := strings.Split(e.Address, "@")
|
||||
if len(parts) > 1 {
|
||||
return parts[1]
|
||||
}
|
||||
return ""
|
||||
})
|
||||
// duplicates: sequence with first email {Address: "user1@example.com"}
|
||||
```
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
name: FindIndexOf
|
||||
slug: findindexof
|
||||
sourceRef: it/find.go#L112
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func FindIndexOf[T any](collection iter.Seq[T], predicate func(item T) bool) (T, int, bool)"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#findindexof
|
||||
similarHelpers:
|
||||
- core#slice#findindexof
|
||||
- it#find#find
|
||||
- it#find#findlastindexof
|
||||
position: 50
|
||||
---
|
||||
|
||||
Searches for an element based on a predicate and returns the element, its index, and true if found. Returns zero value, -1, and false if not found.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(10)
|
||||
_ = yield(20)
|
||||
_ = yield(30)
|
||||
_ = yield(40)
|
||||
}
|
||||
found, index, ok := it.FindIndexOf(seq, func(x int) bool {
|
||||
return x > 25
|
||||
})
|
||||
// found == 30, index == 2, ok == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("cherry")
|
||||
}
|
||||
found, index, ok := it.FindIndexOf(seq, func(s string) bool {
|
||||
return s == "orange"
|
||||
})
|
||||
// found == "", index == -1, ok == false
|
||||
```
|
||||
@@ -0,0 +1,47 @@
|
||||
---
|
||||
name: FindLastIndexOf
|
||||
slug: findlastindexof
|
||||
sourceRef: it/find.go#L127
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func FindLastIndexOf[T any](collection iter.Seq[T], predicate func(item T) bool) (T, int, bool)"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#findlastindexof
|
||||
similarHelpers:
|
||||
- core#slice#findlastindexof
|
||||
- it#find#findindexof
|
||||
- it#find#find
|
||||
position: 60
|
||||
---
|
||||
|
||||
Searches for the last element matching a predicate and returns the element, its index, and true if found. Returns zero value, -1, and false if not found.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(10)
|
||||
_ = yield(20)
|
||||
_ = yield(30)
|
||||
_ = yield(20)
|
||||
_ = yield(40)
|
||||
}
|
||||
found, index, ok := it.FindLastIndexOf(seq, func(x int) bool {
|
||||
return x == 20
|
||||
})
|
||||
// found == 20, index == 3, ok == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("cherry")
|
||||
}
|
||||
found, index, ok := it.FindLastIndexOf(seq, func(s string) bool {
|
||||
return len(s) > 10
|
||||
})
|
||||
// found == "", index == -1, ok == false
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
name: FindOrElse
|
||||
slug: findorelse
|
||||
sourceRef: it/find.go#L147
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func FindOrElse[T any](collection iter.Seq[T], fallback T, predicate func(item T) bool) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#findorelse
|
||||
similarHelpers:
|
||||
- core#slice#findorelse
|
||||
- it#find#find
|
||||
position: 70
|
||||
---
|
||||
|
||||
Searches for an element using a predicate or returns a fallback value if not found.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(10)
|
||||
_ = yield(20)
|
||||
_ = yield(30)
|
||||
_ = yield(40)
|
||||
}
|
||||
result := it.FindOrElse(seq, 99, func(x int) bool {
|
||||
return x > 25
|
||||
})
|
||||
// result == 30
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("cherry")
|
||||
}
|
||||
result := it.FindOrElse(seq, "unknown", func(s string) bool {
|
||||
return len(s) > 10
|
||||
})
|
||||
// result == "unknown" (fallback value)
|
||||
```
|
||||
@@ -0,0 +1,53 @@
|
||||
---
|
||||
name: FindUniques
|
||||
slug: finduniques
|
||||
sourceRef: it/find.go#L159
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func FindUniques[T comparable, I ~func(func(T) bool)](collection I) I"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#finduniques
|
||||
similarHelpers:
|
||||
- core#slice#finduniques
|
||||
- it#find#findduplicates
|
||||
- it#find#finduniquesby
|
||||
position: 80
|
||||
---
|
||||
|
||||
Returns a sequence with elements that appear only once in the original collection (duplicates are removed).
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
_ = yield(4)
|
||||
}
|
||||
uniqueSeq := it.FindUniques(seq)
|
||||
var result []int
|
||||
for v := range uniqueSeq {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains 1, 3 (elements that appear only once)
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("apple")
|
||||
_ = yield("cherry")
|
||||
}
|
||||
uniqueSeq := it.FindUniques(seq)
|
||||
var result []string
|
||||
for v := range uniqueSeq {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains "banana", "cherry" (unique elements)
|
||||
```
|
||||
@@ -0,0 +1,89 @@
|
||||
---
|
||||
name: FindUniquesBy
|
||||
slug: finduniquesby
|
||||
sourceRef: it/find.go#L163
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func FindUniquesBy[T any, U comparable, I ~func(func(T) bool)](collection I, transform func(item T) U) I"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#finduniquesby
|
||||
similarHelpers:
|
||||
- core#slice#finduniquesby
|
||||
position: 630
|
||||
---
|
||||
|
||||
Returns a sequence with all the elements that appear in the collection only once, based on a transform function.
|
||||
|
||||
The order of result values is determined by the order they occur in the collection. A transform function is
|
||||
invoked for each element in the sequence to generate the criterion by which uniqueness is computed.
|
||||
Will iterate through the entire sequence before yielding and allocate a map large enough to hold all distinct transformed elements.
|
||||
Long heterogeneous input sequences can cause excessive memory usage.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Find unique people by age
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 30}, // Same age as Alice, so Alice is not unique
|
||||
{Name: "Diana", Age: 28},
|
||||
})
|
||||
uniques := it.FindUniquesBy(people, func(p Person) int { return p.Age })
|
||||
// uniques: sequence with Bob (age 25) and Diana (age 28)
|
||||
|
||||
// Find unique strings by length
|
||||
words := it.Slice([]string{"hello", "world", "hi", "go", "bye"})
|
||||
uniques := it.FindUniquesBy(words, func(s string) int { return len(s) })
|
||||
// uniques: sequence with words of unique lengths
|
||||
// "hello" (5), "world" (5) - not unique due to same length
|
||||
// "hi" (2), "go" (2), "bye" (3) - "bye" is unique (length 3)
|
||||
|
||||
// Find unique items by first letter
|
||||
items := it.Slice([]string{"apple", "apricot", "banana", "blueberry", "cherry"})
|
||||
uniques := it.FindUniquesBy(items, func(s string) byte { return s[0] })
|
||||
// uniques: sequence with "cherry" (only word starting with 'c')
|
||||
|
||||
// Find unique numbers by modulo
|
||||
numbers := it.Slice([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
|
||||
uniques := it.FindUniquesBy(numbers, func(n int) int { return n % 3 })
|
||||
// uniques: sequence with numbers that have unique remainders when divided by 3
|
||||
|
||||
// Find unique structs by composite key
|
||||
type Order struct {
|
||||
CustomerID string
|
||||
ProductID string
|
||||
}
|
||||
orders := it.Slice([]Order{
|
||||
{CustomerID: "A", ProductID: "1"},
|
||||
{CustomerID: "A", ProductID: "2"},
|
||||
{CustomerID: "B", ProductID: "1"}, // Same customer as first orders
|
||||
{CustomerID: "C", ProductID: "3"},
|
||||
})
|
||||
uniques := it.FindUniquesBy(orders, func(o Order) string { return o.CustomerID })
|
||||
// uniques: sequence with Order{CustomerID: "C", ProductID: "3"} only
|
||||
|
||||
// Find unique items by case-insensitive comparison
|
||||
words := it.Slice([]string{"Hello", "hello", "WORLD", "world", "Go"})
|
||||
uniques := it.FindUniquesBy(words, func(s string) string { return strings.ToLower(s) })
|
||||
// uniques: sequence with "Go" only (others have case-insensitive duplicates)
|
||||
|
||||
// Find unique dates by year-month
|
||||
import "time"
|
||||
dates := it.Slice([]time.Time{
|
||||
time.Date(2023, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
time.Date(2023, 1, 20, 0, 0, 0, 0, time.UTC), // Same month as first
|
||||
time.Date(2023, 2, 10, 0, 0, 0, 0, time.UTC),
|
||||
time.Date(2022, 1, 5, 0, 0, 0, 0, time.UTC), // Different year
|
||||
})
|
||||
uniques := it.FindUniquesBy(dates, func(t time.Time) string {
|
||||
return fmt.Sprintf("%d-%02d", t.Year(), t.Month())
|
||||
})
|
||||
// uniques: sequence with dates from unique year-month combinations
|
||||
```
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
name: First
|
||||
slug: first
|
||||
sourceRef: it/find.go#L362
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func First[T any](collection iter.Seq[T]) (T, bool)"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#first
|
||||
similarHelpers:
|
||||
- core#slice#first
|
||||
- it#find#last
|
||||
- it#find#firstor
|
||||
position: 120
|
||||
---
|
||||
|
||||
Returns the first element of a collection and a boolean indicating availability. Returns zero value and false if the collection is empty.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(10)
|
||||
_ = yield(20)
|
||||
_ = yield(30)
|
||||
}
|
||||
first, ok := it.First(seq)
|
||||
// first == 10, ok == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
// empty sequence
|
||||
}
|
||||
first, ok := it.First(seq)
|
||||
// first == "", ok == false (zero value for string)
|
||||
```
|
||||
@@ -0,0 +1,67 @@
|
||||
---
|
||||
name: FirstOr
|
||||
slug: firstor
|
||||
sourceRef: it/find.go#L377
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func FirstOr[T any](collection iter.Seq[T], fallback T) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#firstor
|
||||
similarHelpers:
|
||||
- core#slice#firstor
|
||||
position: 550
|
||||
---
|
||||
|
||||
Returns the first element of a collection or the fallback value if empty.
|
||||
|
||||
Will iterate at most once.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Get the first element or fallback value
|
||||
numbers := it.Slice([]int{5, 2, 8, 1, 9})
|
||||
first := it.FirstOr(numbers, 42)
|
||||
// first: 5
|
||||
|
||||
// With empty collection
|
||||
empty := it.Slice([]int{})
|
||||
first := it.FirstOr(empty, 42)
|
||||
// first: 42 (fallback value)
|
||||
|
||||
// With strings
|
||||
words := it.Slice([]string{"hello", "world", "go"})
|
||||
first := it.FirstOr(words, "fallback")
|
||||
// first: "hello"
|
||||
|
||||
emptyWords := it.Slice([]string{})
|
||||
first := it.FirstOr(emptyWords, "fallback")
|
||||
// first: "fallback"
|
||||
|
||||
// With structs
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
})
|
||||
first := it.FirstOr(people, Person{Name: "Default", Age: 0})
|
||||
// first: {Name: "Alice", Age: 30}
|
||||
|
||||
emptyPeople := it.Slice([]Person{})
|
||||
first := it.FirstOr(emptyPeople, Person{Name: "Default", Age: 0})
|
||||
// first: {Name: "Default", Age: 0} (fallback value)
|
||||
|
||||
// Using with pointers
|
||||
pointers := it.Slice([]*int{ptr(5), ptr(10), ptr(15)})
|
||||
first := it.FirstOr(pointers, nil)
|
||||
// first: pointer to 5
|
||||
|
||||
emptyPointers := it.Slice([]*int{})
|
||||
first := it.FirstOr(emptyPointers, nil)
|
||||
// first: nil (fallback value)
|
||||
```
|
||||
@@ -0,0 +1,58 @@
|
||||
---
|
||||
name: FirstOrEmpty
|
||||
slug: firstorempty
|
||||
sourceRef: it/find.go#L370
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func FirstOrEmpty[T any](collection iter.Seq[T]) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#firstorempty
|
||||
similarHelpers:
|
||||
- core#slice#firstorempty
|
||||
position: 540
|
||||
---
|
||||
|
||||
Returns the first element of a collection or zero value if empty.
|
||||
|
||||
Will iterate at most once.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Get the first element or zero value
|
||||
numbers := it.Slice([]int{5, 2, 8, 1, 9})
|
||||
first := it.FirstOrEmpty(numbers)
|
||||
// first: 5
|
||||
|
||||
// With empty collection
|
||||
empty := it.Slice([]int{})
|
||||
first := it.FirstOrEmpty(empty)
|
||||
// first: 0 (zero value for int)
|
||||
|
||||
// With strings
|
||||
words := it.Slice([]string{"hello", "world", "go"})
|
||||
first := it.FirstOrEmpty(words)
|
||||
// first: "hello"
|
||||
|
||||
emptyWords := it.Slice([]string{})
|
||||
first := it.FirstOrEmpty(emptyWords)
|
||||
// first: "" (zero value for string)
|
||||
|
||||
// With structs
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
})
|
||||
first := it.FirstOrEmpty(people)
|
||||
// first: {Name: "Alice", Age: 30}
|
||||
|
||||
emptyPeople := it.Slice([]Person{})
|
||||
first := it.FirstOrEmpty(emptyPeople)
|
||||
// first: {Name: "", Age: 0} (zero value for Person)
|
||||
```
|
||||
@@ -0,0 +1,70 @@
|
||||
---
|
||||
name: FlatMap
|
||||
slug: flatmap
|
||||
sourceRef: it/seq.go#L109
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func FlatMap[T, R any](collection iter.Seq[T], transform func(item T) iter.Seq[R]) iter.Seq[R]"
|
||||
- "func FlatMapI[T, R any](collection iter.Seq[T], transform func(item T, index int) iter.Seq[R]) iter.Seq[R]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#sequence#flatmap
|
||||
- it#sequence#flatmapi
|
||||
similarHelpers:
|
||||
- core#slice#flatmap
|
||||
- it#sequence#map
|
||||
position: 120
|
||||
---
|
||||
|
||||
Transforms and flattens a sequence to another type. Each element is transformed into a sequence, then all sequences are concatenated.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func([]int) bool) {
|
||||
_ = yield([]int{1, 2})
|
||||
_ = yield([]int{3, 4})
|
||||
_ = yield([]int{5})
|
||||
}
|
||||
flattened := it.FlatMap(seq, func(arr []int) iter.Seq[int] {
|
||||
return func(yield func(int) bool) {
|
||||
for _, v := range arr {
|
||||
if !yield(v * 2) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
var result []int
|
||||
for v := range flattened {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains 2, 4, 6, 8, 10
|
||||
```
|
||||
|
||||
### FlatMapI
|
||||
|
||||
Transforms and flattens a sequence to another type. Each element is transformed into a sequence with access to the element's index, then all sequences are concatenated.
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("a")
|
||||
_ = yield("b")
|
||||
_ = yield("c")
|
||||
}
|
||||
flattened := it.FlatMapI(seq, func(s string, index int) iter.Seq[string] {
|
||||
return func(yield func(string) bool) {
|
||||
for i := 0; i <= index; i++ {
|
||||
if !yield(fmt.Sprintf("%s-%d", s, i)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
var result []string
|
||||
for v := range flattened {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains "a-0", "b-0", "b-1", "c-0", "c-1", "c-2"
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
name: Flatten
|
||||
slug: flatten
|
||||
sourceRef: it/seq.go#L26
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Flatten[T any, I ~func(func(T) bool)](collection []I) I"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#flatten
|
||||
position: 172
|
||||
---
|
||||
|
||||
Flatten returns a sequence a single level deep.
|
||||
|
||||
```go
|
||||
seq1 := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
}
|
||||
seq2 := func(yield func(int) bool) {
|
||||
yield(3)
|
||||
yield(4)
|
||||
}
|
||||
|
||||
flattened := it.Flatten([]iter.Seq[int]{seq1, seq2})
|
||||
var result []int
|
||||
for item := range flattened {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [1, 2, 3, 4]
|
||||
```
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
name: ForEach
|
||||
slug: foreach
|
||||
sourceRef: it/seq.go#L166
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func ForEach[T any](collection iter.Seq[T], transform func(item T))"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#sequence#foreach
|
||||
- it#sequence#foreachi
|
||||
similarHelpers:
|
||||
- core#slice#foreach
|
||||
- it#sequence#map
|
||||
position: 40
|
||||
---
|
||||
|
||||
Iterates over elements and invokes a transform function for each element.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
}
|
||||
var result []int
|
||||
it.ForEach(seq, func(item int) {
|
||||
result = append(result, item*2)
|
||||
})
|
||||
// result contains 2, 4, 6
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("hello")
|
||||
_ = yield("world")
|
||||
}
|
||||
it.ForEach(seq, func(item string) {
|
||||
fmt.Println("Item:", item)
|
||||
})
|
||||
// Prints: Item: hello
|
||||
// Item: world
|
||||
```
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
name: ForEachWhile
|
||||
slug: foreachwhile
|
||||
sourceRef: it/seq.go#L180
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func ForEachWhile[T any](collection iter.Seq[T], predicate func(item T) bool)"
|
||||
- "func ForEachWhileI[T any](collection iter.Seq[T], predicate func(item T, index int) bool)"
|
||||
variantHelpers:
|
||||
- it#sequence#foreachwhile
|
||||
- it#sequence#foreachwhilei
|
||||
similarHelpers:
|
||||
- it#sequence#foreach
|
||||
position: 160
|
||||
---
|
||||
|
||||
ForEachWhile iterates over elements of collection and invokes predicate for each element.
|
||||
The predicate return value decides to continue or break, like do while().
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
yield(5)
|
||||
}
|
||||
|
||||
called := 0
|
||||
it.ForEachWhile(collection, func(item int) bool {
|
||||
called++
|
||||
return item < 3
|
||||
})
|
||||
// called is 3 (elements 1, 2, 3 were processed)
|
||||
```
|
||||
|
||||
ForEachWhileI iterates over elements of collection and invokes predicate for each element with index.
|
||||
The predicate return value decides to continue or break, like do while().
|
||||
|
||||
```go
|
||||
collection := func(yield func(string) bool) {
|
||||
yield("a")
|
||||
yield("b")
|
||||
yield("c")
|
||||
yield("d")
|
||||
}
|
||||
|
||||
called := 0
|
||||
it.ForEachWhileI(collection, func(item string, index int) bool {
|
||||
called++
|
||||
return index < 2
|
||||
})
|
||||
// called is 3 (elements at indices 0, 1, 2 were processed)
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
name: FromAnySeq
|
||||
slug: fromanyseq
|
||||
sourceRef: it/type_manipulation.go#L11
|
||||
category: it
|
||||
subCategory: type
|
||||
signatures:
|
||||
- "func FromAnySeq[T any](collection iter.Seq[any]) iter.Seq[T]"
|
||||
variantHelpers:
|
||||
- it#type#toanyseq
|
||||
similarHelpers:
|
||||
- core#type#fromany
|
||||
position: 244
|
||||
---
|
||||
|
||||
FromAnySeq returns a sequence with all elements mapped to a type.
|
||||
Panics on type conversion failure.
|
||||
|
||||
```go
|
||||
collection := func(yield func(any) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield("three") // This will cause panic
|
||||
}
|
||||
|
||||
intSeq := it.FromAnySeq[int](collection)
|
||||
// This will panic when trying to convert "three" to int
|
||||
```
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
name: FromEntries
|
||||
slug: fromentries
|
||||
sourceRef: it/map.go#L96
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func FromEntries[K comparable, V any](entries ...iter.Seq2[K, V]) map[K]V"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#map#fromentries
|
||||
- it#map#frompairs
|
||||
similarHelpers:
|
||||
- core#slice#fromentries
|
||||
- it#map#entries
|
||||
position: 30
|
||||
---
|
||||
|
||||
Transforms a sequence of key/value pairs into a map. Accepts multiple sequences and merges them.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
entries := func(yield func(string, int) bool) {
|
||||
_ = yield("apple", 1)
|
||||
_ = yield("banana", 2)
|
||||
_ = yield("cherry", 3)
|
||||
}
|
||||
m := it.FromEntries(entries)
|
||||
// m == map[string]int{"apple": 1, "banana": 2, "cherry": 3}
|
||||
```
|
||||
|
||||
```go
|
||||
entries1 := func(yield func(string, int) bool) {
|
||||
_ = yield("a", 1)
|
||||
_ = yield("b", 2)
|
||||
}
|
||||
entries2 := func(yield func(string, int) bool) {
|
||||
_ = yield("c", 3)
|
||||
_ = yield("d", 4)
|
||||
}
|
||||
m := it.FromEntries(entries1, entries2)
|
||||
// m contains all entries from both sequences
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
name: FromSeqPtr
|
||||
slug: fromseqptr
|
||||
sourceRef: it/type_manipulation.go#L11
|
||||
category: it
|
||||
subCategory: type
|
||||
signatures:
|
||||
- "func FromSeqPtr[T any](collection iter.Seq[*T]) iter.Seq[T]"
|
||||
variantHelpers:
|
||||
- it#type#fromseqptror
|
||||
similarHelpers:
|
||||
- core#type#fromptr
|
||||
position: 241
|
||||
---
|
||||
|
||||
FromSeqPtr returns a sequence with the pointer values.
|
||||
Returns a zero value in case of a nil pointer element.
|
||||
|
||||
```go
|
||||
one := 1
|
||||
two := 2
|
||||
var three *int = nil
|
||||
|
||||
collection := func(yield func(*int) bool) {
|
||||
yield(&one)
|
||||
yield(&two)
|
||||
yield(three)
|
||||
}
|
||||
|
||||
values := it.FromSeqPtr(collection)
|
||||
var result []int
|
||||
for val := range values {
|
||||
result = append(result, val)
|
||||
}
|
||||
// result contains [1, 2, 0]
|
||||
```
|
||||
@@ -0,0 +1,32 @@
|
||||
---
|
||||
name: FromSeqPtrOr
|
||||
slug: fromseqptror
|
||||
sourceRef: it/type_manipulation.go#L11
|
||||
category: it
|
||||
subCategory: type
|
||||
signatures:
|
||||
- "func FromSeqPtrOr[T any](collection iter.Seq[*T], fallback T) iter.Seq[T]"
|
||||
variantHelpers:
|
||||
- it#type#fromseqptr
|
||||
similarHelpers: []
|
||||
position: 242
|
||||
---
|
||||
|
||||
FromSeqPtrOr returns a sequence with the pointer values or the fallback value.
|
||||
|
||||
```go
|
||||
one := 1
|
||||
var two *int = nil
|
||||
|
||||
collection := func(yield func(*int) bool) {
|
||||
yield(&one)
|
||||
yield(two)
|
||||
}
|
||||
|
||||
values := it.FromSeqPtrOr(collection, 99)
|
||||
var result []int
|
||||
for val := range values {
|
||||
result = append(result, val)
|
||||
}
|
||||
// result contains [1, 99]
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
name: GroupBy
|
||||
slug: groupby
|
||||
sourceRef: it/seq.go#L244
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func GroupBy[T any, U comparable](collection iter.Seq[T], transform func(item T) U) map[U][]T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#sequence#groupby
|
||||
similarHelpers:
|
||||
- core#slice#groupby
|
||||
- it#sequence#partitionby
|
||||
- it#sequence#groupbymap
|
||||
position: 80
|
||||
---
|
||||
|
||||
Returns an object composed of keys generated from running each element of collection through a transform function. The value of each key is an array of elements responsible for generating the key.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("apricot")
|
||||
_ = yield("blueberry")
|
||||
}
|
||||
grouped := it.GroupBy(seq, func(s string) string {
|
||||
return string(s[0]) // group by first letter
|
||||
})
|
||||
// grouped contains map with keys: "a": ["apple", "apricot"], "b": ["banana", "blueberry"]
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
name: HasPrefix
|
||||
slug: hasprefix
|
||||
sourceRef: it/find.go#L49
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func HasPrefix[T comparable](collection iter.Seq[T], prefix ...T) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#hasprefix
|
||||
similarHelpers:
|
||||
- core#slice#hasprefix
|
||||
- it#find#hassuffix
|
||||
position: 20
|
||||
---
|
||||
|
||||
Returns true if the collection has the specified prefix. The prefix can be specified as multiple arguments.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
}
|
||||
hasPrefix := it.HasPrefix(seq, 1, 2)
|
||||
// hasPrefix == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("hello")
|
||||
_ = yield("world")
|
||||
}
|
||||
hasPrefix := it.HasPrefix(seq, "hello")
|
||||
// hasPrefix == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
}
|
||||
hasPrefix := it.HasPrefix(seq, 2, 3)
|
||||
// hasPrefix == false
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
name: HasSuffix
|
||||
slug: hassuffix
|
||||
sourceRef: it/find.go#L71
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func HasSuffix[T comparable](collection iter.Seq[T], suffix ...T) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#hassuffix
|
||||
similarHelpers:
|
||||
- core#slice#hassuffix
|
||||
- it#find#hasprefix
|
||||
position: 30
|
||||
---
|
||||
|
||||
Returns true if the collection has the specified suffix. The suffix can be specified as multiple arguments.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
}
|
||||
hasSuffix := it.HasSuffix(seq, 3, 4)
|
||||
// hasSuffix == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("hello")
|
||||
_ = yield("world")
|
||||
}
|
||||
hasSuffix := it.HasSuffix(seq, "world")
|
||||
// hasSuffix == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
}
|
||||
hasSuffix := it.HasSuffix(seq, 1, 2)
|
||||
// hasSuffix == false
|
||||
```
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
name: IndexOf
|
||||
slug: indexof
|
||||
sourceRef: it/find.go#L19
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func IndexOf[T comparable](collection iter.Seq[T], element T) int"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#indexof
|
||||
similarHelpers:
|
||||
- core#slice#indexof
|
||||
- it#find#lastindexof
|
||||
position: 0
|
||||
---
|
||||
|
||||
Returns the index at which the first occurrence of a value is found in the sequence, or -1 if the value is not found.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(10)
|
||||
_ = yield(20)
|
||||
_ = yield(30)
|
||||
_ = yield(20)
|
||||
}
|
||||
idx := it.IndexOf(seq, 20)
|
||||
// idx == 1
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("cherry")
|
||||
}
|
||||
idx := it.IndexOf(seq, "orange")
|
||||
// idx == -1
|
||||
```
|
||||
@@ -0,0 +1,37 @@
|
||||
---
|
||||
name: Interleave
|
||||
slug: interleave
|
||||
sourceRef: it/seq.go#L26
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Interleave[T any](collections ...iter.Seq[T]) iter.Seq[T]"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#interleave
|
||||
position: 173
|
||||
---
|
||||
|
||||
Interleave round-robin alternating input sequences and sequentially appending value at index into result.
|
||||
|
||||
```go
|
||||
seq1 := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(3)
|
||||
}
|
||||
seq2 := func(yield func(int) bool) {
|
||||
yield(2)
|
||||
yield(4)
|
||||
}
|
||||
seq3 := func(yield func(int) bool) {
|
||||
yield(5)
|
||||
yield(6)
|
||||
}
|
||||
|
||||
interleaved := it.Interleave(seq1, seq2, seq3)
|
||||
var result []int
|
||||
for item := range interleaved {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [1, 2, 5, 3, 4, 6]
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
name: Intersect
|
||||
slug: intersect
|
||||
sourceRef: it/intersect.go#L78
|
||||
category: it
|
||||
subCategory: intersect
|
||||
signatures:
|
||||
- "func Intersect[T comparable, I ~func(func(T) bool)](lists ...I) I"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#intersect#intersect
|
||||
similarHelpers:
|
||||
- core#slice#intersect
|
||||
- it#intersect#union
|
||||
position: 10
|
||||
---
|
||||
|
||||
Returns the intersection between given collections (elements present in all collections).
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq1 := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
}
|
||||
seq2 := func(yield func(int) bool) {
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(5)
|
||||
}
|
||||
seq3 := func(yield func(int) bool) {
|
||||
_ = yield(3)
|
||||
_ = yield(2)
|
||||
_ = yield(6)
|
||||
}
|
||||
intersection := it.Intersect(seq1, seq2, seq3)
|
||||
var result []int
|
||||
for v := range intersection {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains 2, 3 (elements present in all sequences)
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
name: Invert
|
||||
slug: invert
|
||||
sourceRef: it/map.go#L111
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func Invert[K, V comparable](in iter.Seq2[K, V]) iter.Seq2[V, K]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#map#invert
|
||||
similarHelpers:
|
||||
- core#slice#invert
|
||||
- it#map#entries
|
||||
position: 40
|
||||
---
|
||||
|
||||
Creates a sequence composed of inverted keys and values from a sequence of key/value pairs.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
entries := func(yield func(string, int) bool) {
|
||||
_ = yield("apple", 1)
|
||||
_ = yield("banana", 2)
|
||||
_ = yield("cherry", 3)
|
||||
}
|
||||
inverted := it.Invert(entries)
|
||||
var keys []int
|
||||
var values []string
|
||||
for k, v := range inverted {
|
||||
keys = append(keys, k)
|
||||
values = append(values, v)
|
||||
}
|
||||
// keys contains 1, 2, 3 and values contains "apple", "banana", "cherry"
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: IsEmpty
|
||||
slug: isempty
|
||||
sourceRef: it/type_manipulation.go#L50
|
||||
category: it
|
||||
subCategory: type
|
||||
signatures:
|
||||
- "func IsEmpty[T any](collection iter.Seq[T]) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#type#isempty
|
||||
similarHelpers:
|
||||
- it#type#isnotempty
|
||||
- it#type#empty
|
||||
- it#sequence#length
|
||||
position: 10
|
||||
---
|
||||
|
||||
Returns true if the sequence is empty. Will consume the entire sequence to check.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
// empty sequence
|
||||
}
|
||||
empty := it.IsEmpty(seq)
|
||||
// empty == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
}
|
||||
empty := it.IsEmpty(seq)
|
||||
// empty == false
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
name: IsSorted
|
||||
slug: issorted
|
||||
sourceRef: it/seq.go#L720
|
||||
category: it
|
||||
subCategory: slice
|
||||
signatures:
|
||||
- "func IsSorted[T constraints.Ordered](collection iter.Seq[T]) bool"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#issorted
|
||||
position: 200
|
||||
---
|
||||
|
||||
IsSorted checks if a sequence is sorted.
|
||||
|
||||
```go
|
||||
sorted := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
}
|
||||
unsorted := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(3)
|
||||
yield(2)
|
||||
yield(4)
|
||||
}
|
||||
|
||||
fmt.Println(it.IsSorted(sorted)) // true
|
||||
fmt.Println(it.IsSorted(unsorted)) // false
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
name: IsSortedBy
|
||||
slug: issortedby
|
||||
sourceRef: it/seq.go#L720
|
||||
category: it
|
||||
subCategory: slice
|
||||
signatures:
|
||||
- "func IsSortedBy[T any, K constraints.Ordered](collection iter.Seq[T], transform func(item T) K) bool"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#issortedby
|
||||
position: 201
|
||||
---
|
||||
|
||||
IsSortedBy checks if a sequence is sorted by transform.
|
||||
|
||||
```go
|
||||
collection := func(yield func(string) bool) {
|
||||
yield("apple")
|
||||
yield("banana")
|
||||
yield("cherry")
|
||||
}
|
||||
|
||||
sortedByLength := it.IsSortedBy(collection, func(s string) int {
|
||||
return len(s)
|
||||
})
|
||||
// true (5, 6, 6 is sorted)
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
name: Keyify
|
||||
slug: keyify
|
||||
sourceRef: it/seq.go#L720
|
||||
category: it
|
||||
subCategory: slice
|
||||
signatures:
|
||||
- "func Keyify[T comparable](collection iter.Seq[T]) map[T]struct{}"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#keyby
|
||||
position: 202
|
||||
---
|
||||
|
||||
Keyify returns a map with each unique element of the sequence as a key.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(1)
|
||||
yield(3)
|
||||
yield(2)
|
||||
}
|
||||
|
||||
keyMap := it.Keyify(collection)
|
||||
// keyMap contains {1: {}, 2: {}, 3: {}}
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: Keys
|
||||
slug: keys
|
||||
sourceRef: it/map.go#L11
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func Keys[K comparable, V any](in ...map[K]V) iter.Seq[K]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#map#keys
|
||||
similarHelpers:
|
||||
- core#slice#keys
|
||||
- it#map#values
|
||||
- it#map#uniqkeys
|
||||
position: 0
|
||||
---
|
||||
|
||||
Creates a sequence of the map keys. Accepts multiple maps and concatenates their keys.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
m1 := map[string]int{
|
||||
"apple": 1,
|
||||
"banana": 2,
|
||||
}
|
||||
m2 := map[string]int{
|
||||
"cherry": 3,
|
||||
"date": 4,
|
||||
}
|
||||
keysSeq := it.Keys(m1, m2)
|
||||
var result []string
|
||||
for k := range keysSeq {
|
||||
result = append(result, k)
|
||||
}
|
||||
// result contains keys from both maps
|
||||
```
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
name: Last
|
||||
slug: last
|
||||
sourceRef: it/find.go#L389
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func Last[T any](collection iter.Seq[T]) (T, bool)"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#last
|
||||
similarHelpers:
|
||||
- core#slice#last
|
||||
- it#find#first
|
||||
- it#find#lastor
|
||||
position: 130
|
||||
---
|
||||
|
||||
Returns the last element of a collection and a boolean indicating availability. Returns zero value and false if the collection is empty.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(10)
|
||||
_ = yield(20)
|
||||
_ = yield(30)
|
||||
}
|
||||
last, ok := it.Last(seq)
|
||||
// last == 30, ok == true
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
// empty sequence
|
||||
}
|
||||
last, ok := it.Last(seq)
|
||||
// last == "", ok == false (zero value for string)
|
||||
```
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
name: LastIndexOf
|
||||
slug: lastindexof
|
||||
sourceRef: it/find.go#L34
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func LastIndexOf[T comparable](collection iter.Seq[T], element T) int"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#lastindexof
|
||||
similarHelpers:
|
||||
- core#slice#lastindexof
|
||||
- it#find#indexof
|
||||
position: 10
|
||||
---
|
||||
|
||||
Returns the index at which the last occurrence of a value is found in the sequence, or -1 if the value is not found.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(10)
|
||||
_ = yield(20)
|
||||
_ = yield(30)
|
||||
_ = yield(20)
|
||||
}
|
||||
idx := it.LastIndexOf(seq, 20)
|
||||
// idx == 3
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
_ = yield("cherry")
|
||||
}
|
||||
idx := it.LastIndexOf(seq, "orange")
|
||||
// idx == -1
|
||||
```
|
||||
@@ -0,0 +1,72 @@
|
||||
---
|
||||
name: LastOr
|
||||
slug: lastor
|
||||
sourceRef: it/find.go#L407
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func LastOr[T any](collection iter.Seq[T], fallback T) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#lastor
|
||||
similarHelpers:
|
||||
- core#slice#lastor
|
||||
position: 570
|
||||
---
|
||||
|
||||
Returns the last element of a collection or the fallback value if empty.
|
||||
|
||||
Will iterate through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Get the last element or fallback value
|
||||
numbers := it.Slice([]int{5, 2, 8, 1, 9})
|
||||
last := it.LastOr(numbers, 42)
|
||||
// last: 9
|
||||
|
||||
// With empty collection
|
||||
empty := it.Slice([]int{})
|
||||
last := it.LastOr(empty, 42)
|
||||
// last: 42 (fallback value)
|
||||
|
||||
// With strings
|
||||
words := it.Slice([]string{"hello", "world", "go"})
|
||||
last := it.LastOr(words, "fallback")
|
||||
// last: "go"
|
||||
|
||||
emptyWords := it.Slice([]string{})
|
||||
last := it.LastOr(emptyWords, "fallback")
|
||||
// last: "fallback"
|
||||
|
||||
// With structs
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
})
|
||||
last := it.LastOr(people, Person{Name: "Default", Age: 0})
|
||||
// last: {Name: "Bob", Age: 25}
|
||||
|
||||
emptyPeople := it.Slice([]Person{})
|
||||
last := it.LastOr(emptyPeople, Person{Name: "Default", Age: 0})
|
||||
// last: {Name: "Default", Age: 0} (fallback value)
|
||||
|
||||
// With single element
|
||||
single := it.Slice([]int{42})
|
||||
last := it.LastOr(single, 99)
|
||||
// last: 42
|
||||
|
||||
// Using with nil pointer fallback
|
||||
values := it.Slice([]*string{ptr("hello"), ptr("world")})
|
||||
last := it.LastOr(values, nil)
|
||||
// last: pointer to "world"
|
||||
|
||||
emptyValues := it.Slice([]*string{})
|
||||
last := it.LastOr(emptyValues, nil)
|
||||
// last: nil (fallback value)
|
||||
```
|
||||
@@ -0,0 +1,63 @@
|
||||
---
|
||||
name: LastOrEmpty
|
||||
slug: lastorempty
|
||||
sourceRef: it/find.go#L400
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func LastOrEmpty[T any](collection iter.Seq[T]) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#lastorempty
|
||||
similarHelpers:
|
||||
- core#slice#lastorempty
|
||||
position: 560
|
||||
---
|
||||
|
||||
Returns the last element of a collection or zero value if empty.
|
||||
|
||||
Will iterate through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Get the last element or zero value
|
||||
numbers := it.Slice([]int{5, 2, 8, 1, 9})
|
||||
last := it.LastOrEmpty(numbers)
|
||||
// last: 9
|
||||
|
||||
// With empty collection
|
||||
empty := it.Slice([]int{})
|
||||
last := it.LastOrEmpty(empty)
|
||||
// last: 0 (zero value for int)
|
||||
|
||||
// With strings
|
||||
words := it.Slice([]string{"hello", "world", "go"})
|
||||
last := it.LastOrEmpty(words)
|
||||
// last: "go"
|
||||
|
||||
emptyWords := it.Slice([]string{})
|
||||
last := it.LastOrEmpty(emptyWords)
|
||||
// last: "" (zero value for string)
|
||||
|
||||
// With structs
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
})
|
||||
last := it.LastOrEmpty(people)
|
||||
// last: {Name: "Bob", Age: 25}
|
||||
|
||||
emptyPeople := it.Slice([]Person{})
|
||||
last := it.LastOrEmpty(emptyPeople)
|
||||
// last: {Name: "", Age: 0} (zero value for Person)
|
||||
|
||||
// With single element
|
||||
single := it.Slice([]int{42})
|
||||
last := it.LastOrEmpty(single)
|
||||
// last: 42
|
||||
```
|
||||
@@ -0,0 +1,58 @@
|
||||
---
|
||||
name: Latest
|
||||
slug: latest
|
||||
sourceRef: it/find.go#L346
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func Latest(times iter.Seq[time.Time]) time.Time"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#latest
|
||||
similarHelpers:
|
||||
- core#slice#latest
|
||||
position: 520
|
||||
---
|
||||
|
||||
Searches for the latest (maximum) time.Time in a collection.
|
||||
|
||||
Returns zero value when the collection is empty.
|
||||
Will iterate through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
import "time"
|
||||
|
||||
// Find the latest time from a collection
|
||||
times := it.Slice([]time.Time{
|
||||
time.Date(2023, 5, 15, 10, 0, 0, 0, time.UTC),
|
||||
time.Date(2023, 3, 20, 14, 30, 0, 0, time.UTC),
|
||||
time.Date(2023, 8, 1, 9, 15, 0, 0, time.UTC),
|
||||
})
|
||||
latest := it.Latest(times)
|
||||
// latest: 2023-08-01 09:15:00 +0000 UTC
|
||||
|
||||
// With empty collection
|
||||
empty := it.Slice([]time.Time{})
|
||||
latest := it.Latest(empty)
|
||||
// latest: 0001-01-01 00:00:00 +0000 UTC (zero value)
|
||||
|
||||
// Find latest from parsed times
|
||||
times := it.Slice([]time.Time{
|
||||
time.Parse(time.RFC3339, "2023-01-01T12:00:00Z"),
|
||||
time.Parse(time.RFC3339, "2023-01-01T10:00:00Z"),
|
||||
time.Parse(time.RFC3339, "2023-01-01T14:00:00Z"),
|
||||
})
|
||||
latest := it.Latest(times)
|
||||
// latest: 2023-01-01 14:00:00 +0000 UTC
|
||||
|
||||
// Find latest log entry timestamp
|
||||
logs := it.Slice([]time.Time{
|
||||
time.Now().Add(-2 * time.Hour),
|
||||
time.Now().Add(-1 * time.Hour),
|
||||
time.Now(),
|
||||
})
|
||||
latest := it.Latest(logs)
|
||||
// latest: current time
|
||||
```
|
||||
@@ -0,0 +1,73 @@
|
||||
---
|
||||
name: LatestBy
|
||||
slug: latestby
|
||||
sourceRef: it/find.go#L353
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func LatestBy[T any](collection iter.Seq[T], transform func(item T) time.Time) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#latestby
|
||||
similarHelpers:
|
||||
- core#slice#latestby
|
||||
position: 530
|
||||
---
|
||||
|
||||
Searches for the element with the latest time using a transform function.
|
||||
|
||||
Returns zero value when the collection is empty.
|
||||
Will iterate through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
import "time"
|
||||
|
||||
type Event struct {
|
||||
Name string
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
// Find the latest event by time
|
||||
events := it.Slice([]Event{
|
||||
{"Meeting", time.Date(2023, 5, 15, 10, 0, 0, 0, time.UTC)},
|
||||
{"Lunch", time.Date(2023, 5, 15, 12, 0, 0, 0, time.UTC)},
|
||||
{"Breakfast", time.Date(2023, 5, 15, 8, 0, 0, 0, time.UTC)},
|
||||
})
|
||||
latest := it.LatestBy(events, func(e Event) time.Time {
|
||||
return e.Time
|
||||
})
|
||||
// latest: {Name: "Lunch", Time: 2023-05-15 12:00:00 +0000 UTC}
|
||||
|
||||
// Find the latest task by deadline
|
||||
type Task struct {
|
||||
ID int
|
||||
Deadline time.Time
|
||||
}
|
||||
tasks := it.Slice([]Task{
|
||||
{1, time.Date(2023, 6, 1, 0, 0, 0, 0, time.UTC)},
|
||||
{2, time.Date(2023, 5, 15, 0, 0, 0, 0, time.UTC)},
|
||||
{3, time.Date(2023, 7, 1, 0, 0, 0, 0, time.UTC)},
|
||||
})
|
||||
latest := it.LatestBy(tasks, func(t Task) time.Time {
|
||||
return t.Deadline
|
||||
})
|
||||
// latest: {ID: 3, Deadline: 2023-07-01 00:00:00 +0000 UTC}
|
||||
|
||||
// Find the most recent activity
|
||||
type Activity struct {
|
||||
User string
|
||||
Action string
|
||||
Time time.Time
|
||||
}
|
||||
activities := it.Slice([]Activity{
|
||||
{"alice", "login", time.Now().Add(-24 * time.Hour)},
|
||||
{"bob", "logout", time.Now().Add(-12 * time.Hour)},
|
||||
{"alice", "post", time.Now().Add(-1 * time.Hour)},
|
||||
})
|
||||
latest := it.LatestBy(activities, func(a Activity) time.Time {
|
||||
return a.Time
|
||||
})
|
||||
// latest: {User: "alice", Action: "post", Time: 1 hour ago}
|
||||
```
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
name: Length
|
||||
slug: length
|
||||
sourceRef: it/seq.go#L16
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Length[T any](collection iter.Seq[T]) int"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#sequence#length
|
||||
similarHelpers:
|
||||
- core#slice#length
|
||||
- it#sequence#isempty
|
||||
- it#sequence#isnotempty
|
||||
position: 0
|
||||
---
|
||||
|
||||
Returns the length of a collection by iterating through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
}
|
||||
length := it.Length(seq)
|
||||
// length == 3
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
// empty sequence
|
||||
}
|
||||
length := it.Length(seq)
|
||||
// length == 0
|
||||
```
|
||||
@@ -0,0 +1,59 @@
|
||||
---
|
||||
name: Map
|
||||
slug: map
|
||||
sourceRef: it/seq.go#L51
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Map[T, R any](collection iter.Seq[T], transform func(item T) R) iter.Seq[R]"
|
||||
- "func MapI[T, R any](collection iter.Seq[T], transform func(item T, index int) R) iter.Seq[R]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#sequence#map
|
||||
- it#sequence#mapi
|
||||
similarHelpers:
|
||||
- core#slice#map
|
||||
- it#sequence#filtermap
|
||||
- it#sequence#flatmap
|
||||
position: 20
|
||||
---
|
||||
|
||||
Transforms a sequence to another type by applying a transform function to each element.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
}
|
||||
mapped := it.Map(seq, func(x int) string {
|
||||
return fmt.Sprintf("item-%d", x)
|
||||
})
|
||||
var result []string
|
||||
for v := range mapped {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains "item-1", "item-2", "item-3"
|
||||
```
|
||||
|
||||
### MapI
|
||||
|
||||
Transforms a sequence to another type by applying a transform function to each element and its index.
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(10)
|
||||
_ = yield(20)
|
||||
_ = yield(30)
|
||||
}
|
||||
mapped := it.MapI(seq, func(x int, index int) string {
|
||||
return fmt.Sprintf("item-%d-%d", x, index)
|
||||
})
|
||||
var result []string
|
||||
for v := range mapped {
|
||||
result = append(result, v)
|
||||
}
|
||||
// result contains "item-10-0", "item-20-1", "item-30-2"
|
||||
```
|
||||
@@ -0,0 +1,48 @@
|
||||
---
|
||||
name: MapToSeq
|
||||
slug: maptoseq
|
||||
sourceRef: it/map.go#L164
|
||||
category: it
|
||||
subCategory: map
|
||||
signatures:
|
||||
- "func MapToSeq[K comparable, V, R any](in map[K]V, transform func(key K, value V) R) iter.Seq[R]"
|
||||
variantHelpers:
|
||||
- it#map#maptoseq
|
||||
similarHelpers:
|
||||
- core#map#maptoslice
|
||||
- it#map#values
|
||||
- it#map#keys
|
||||
- it#map#entries
|
||||
- it#map#filtermaptoseq
|
||||
position: 52
|
||||
---
|
||||
|
||||
Transforms a map into a sequence by applying a transform function to each key-value pair. The transform function determines what values are yielded in the output sequence.
|
||||
|
||||
```go
|
||||
m := map[string]int{
|
||||
"apple": 3,
|
||||
"banana": 5,
|
||||
"cherry": 2,
|
||||
}
|
||||
result := lo.MapToSeq(m, func(key string, value int) string {
|
||||
return fmt.Sprintf("%s:%d", key, value)
|
||||
})
|
||||
// iter.Seq[string] yielding "apple:3", "banana:5", "cherry:2"
|
||||
|
||||
numberMap := map[int]string{1: "one", 2: "two", 3: "three"}
|
||||
result = lo.MapToSeq(numberMap, func(key int, value string) int {
|
||||
return key * len(value)
|
||||
})
|
||||
// iter.Seq[int] yielding 3, 6, 15 (1*3, 2*3, 3*5)
|
||||
|
||||
personMap := map[string]int{"alice": 25, "bob": 30}
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
result = lo.MapToSeq(personMap, func(name string, age int) Person {
|
||||
return Person{Name: name, Age: age}
|
||||
})
|
||||
// iter.Seq[Person] yielding {Name: "alice", Age: 25}, {Name: "bob", Age: 30}
|
||||
```
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
name: Max
|
||||
slug: max
|
||||
sourceRef: it/find.go#L295
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func Max[T constraints.Ordered](collection iter.Seq[T]) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#max
|
||||
similarHelpers:
|
||||
- core#slice#max
|
||||
- it#find#min
|
||||
- it#find#maxby
|
||||
position: 110
|
||||
---
|
||||
|
||||
Searches the maximum value of a collection. Returns the largest element found.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(5)
|
||||
_ = yield(2)
|
||||
_ = yield(8)
|
||||
_ = yield(1)
|
||||
_ = yield(9)
|
||||
}
|
||||
max := it.Max(seq)
|
||||
// max == 9
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("zebra")
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
}
|
||||
max := it.Max(seq)
|
||||
// max == "zebra" (lexicographically largest)
|
||||
```
|
||||
@@ -0,0 +1,37 @@
|
||||
---
|
||||
name: MaxBy
|
||||
slug: maxby
|
||||
sourceRef: it/find.go#L310
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func MaxBy[T any](collection iter.Seq[T], comparison func(a, b T) bool) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#maxby
|
||||
similarHelpers:
|
||||
- core#slice#maxby
|
||||
- it#find#minby
|
||||
- it#find#max
|
||||
position: 150
|
||||
---
|
||||
|
||||
Searches maximum value using a custom comparison function. The comparison function should return true if the first argument is "greater than" the second.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
seq := func(yield func(Person) bool) {
|
||||
_ = yield(Person{"Alice", 30})
|
||||
_ = yield(Person{"Bob", 25})
|
||||
_ = yield(Person{"Charlie", 35})
|
||||
}
|
||||
oldest := it.MaxBy(seq, func(a, b Person) bool {
|
||||
return a.Age > b.Age
|
||||
})
|
||||
// oldest == Person{"Charlie", 35}
|
||||
```
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
name: MaxIndex
|
||||
slug: maxindex
|
||||
sourceRef: it/find.go#L299
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func MaxIndex[T constraints.Ordered](collection iter.Seq[T]) (T, int)"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#maxindex
|
||||
similarHelpers:
|
||||
- core#slice#maxindex
|
||||
position: 480
|
||||
---
|
||||
|
||||
Searches the maximum value of a collection and returns both the value and its index.
|
||||
|
||||
Returns (zero value, -1) when the collection is empty.
|
||||
Will iterate through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Find the maximum value and its index
|
||||
numbers := it.Slice([]int{5, 2, 8, 1, 9})
|
||||
value, index := it.MaxIndex(numbers)
|
||||
// value: 9, index: 4
|
||||
|
||||
// With empty collection
|
||||
empty := it.Slice([]int{})
|
||||
value, index := it.MaxIndex(empty)
|
||||
// value: 0, index: -1
|
||||
|
||||
// Find the maximum string alphabetically and its index
|
||||
words := it.Slice([]string{"apple", "zebra", "banana", "xylophone"})
|
||||
value, index := it.MaxIndex(words)
|
||||
// value: "zebra", index: 1
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
name: MaxIndexBy
|
||||
slug: maxindexby
|
||||
sourceRef: it/find.go#L326
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func MaxIndexBy[T any](collection iter.Seq[T], comparison func(a, b T) bool) (T, int)"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#maxindexby
|
||||
similarHelpers:
|
||||
- core#slice#maxindexby
|
||||
position: 490
|
||||
---
|
||||
|
||||
Searches the maximum value of a collection using a comparison function and returns both the value and its index.
|
||||
|
||||
If several values are equal to the greatest value, returns the first such value.
|
||||
Returns (zero value, -1) when the collection is empty.
|
||||
Will iterate through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Find the maximum string by length and its index
|
||||
words := it.Slice([]string{"apple", "hi", "banana", "xylophone"})
|
||||
value, index := it.MaxIndexBy(words, func(a, b string) bool {
|
||||
return len(a) > len(b)
|
||||
})
|
||||
// value: "xylophone", index: 3
|
||||
|
||||
// Find the maximum person by age and its index
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 35},
|
||||
})
|
||||
value, index := it.MaxIndexBy(people, func(a, b Person) bool {
|
||||
return a.Age > b.Age
|
||||
})
|
||||
// value: {Name: "Charlie", Age: 35}, index: 2
|
||||
|
||||
// Find the maximum number by absolute value and its index
|
||||
numbers := it.Slice([]int{-5, 2, -8, 1})
|
||||
value, index := it.MaxIndexBy(numbers, func(a, b int) bool {
|
||||
return abs(a) > abs(b)
|
||||
})
|
||||
// value: -8, index: 2
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
name: Mean / MeanBy
|
||||
slug: mean
|
||||
sourceRef: it/math.go#L80
|
||||
category: it
|
||||
subCategory: math
|
||||
signatures:
|
||||
- "func Mean[T constraints.Float | constraints.Integer](collection iter.Seq[T]) T"
|
||||
- "func MeanBy[T any, R constraints.Float | constraints.Integer](collection iter.Seq[T], iteratee func(item T) R) R"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#math#mean
|
||||
- it#math#meanby
|
||||
similarHelpers:
|
||||
- core#slice#mean
|
||||
- core#slice#meanby
|
||||
position: 30
|
||||
---
|
||||
|
||||
Computes the arithmetic mean. `MeanBy` applies a transform before averaging. Returns 0 for empty sequences.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
avg := it.Mean(iter.Seq[int](func(y func(int) bool){ _ = y(2); _ = y(3); _ = y(5) }))
|
||||
// avg == 10/3 == 3 (int division)
|
||||
```
|
||||
|
||||
```go
|
||||
avg := it.MeanBy(iter.Seq[string](func(y func(string) bool){ _ = y("aa"); _ = y("bbb") }), func(s string) int { return len(s) })
|
||||
// (2+3)/2 == 2
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
name: Min
|
||||
slug: min
|
||||
sourceRef: it/find.go#L227
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func Min[T constraints.Ordered](collection iter.Seq[T]) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#min
|
||||
similarHelpers:
|
||||
- core#slice#min
|
||||
- it#find#max
|
||||
- it#find#minby
|
||||
position: 100
|
||||
---
|
||||
|
||||
Searches the minimum value of a collection. Returns the smallest element found.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(5)
|
||||
_ = yield(2)
|
||||
_ = yield(8)
|
||||
_ = yield(1)
|
||||
_ = yield(9)
|
||||
}
|
||||
min := it.Min(seq)
|
||||
// min == 1
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("zebra")
|
||||
_ = yield("apple")
|
||||
_ = yield("banana")
|
||||
}
|
||||
min := it.Min(seq)
|
||||
// min == "apple" (lexicographically smallest)
|
||||
```
|
||||
@@ -0,0 +1,37 @@
|
||||
---
|
||||
name: MinBy
|
||||
slug: minby
|
||||
sourceRef: it/find.go#L242
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func MinBy[T any](collection iter.Seq[T], comparison func(a, b T) bool) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#minby
|
||||
similarHelpers:
|
||||
- core#slice#minby
|
||||
- it#find#maxby
|
||||
- it#find#min
|
||||
position: 140
|
||||
---
|
||||
|
||||
Searches minimum value using a custom comparison function. The comparison function should return true if the first argument is "less than" the second.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
seq := func(yield func(Person) bool) {
|
||||
_ = yield(Person{"Alice", 30})
|
||||
_ = yield(Person{"Bob", 25})
|
||||
_ = yield(Person{"Charlie", 35})
|
||||
}
|
||||
youngest := it.MinBy(seq, func(a, b Person) bool {
|
||||
return a.Age < b.Age
|
||||
})
|
||||
// youngest == Person{"Bob", 25}
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
name: MinIndex
|
||||
slug: minindex
|
||||
sourceRef: it/find.go#L231
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func MinIndex[T constraints.Ordered](collection iter.Seq[T]) (T, int)"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#minindex
|
||||
similarHelpers:
|
||||
- core#slice#minindex
|
||||
position: 460
|
||||
---
|
||||
|
||||
Searches the minimum value of a collection and returns both the value and its index.
|
||||
|
||||
Returns (zero value, -1) when the collection is empty.
|
||||
Will iterate through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Find the minimum value and its index
|
||||
numbers := it.Slice([]int{5, 2, 8, 1, 9})
|
||||
value, index := it.MinIndex(numbers)
|
||||
// value: 1, index: 3
|
||||
|
||||
// With empty collection
|
||||
empty := it.Slice([]int{})
|
||||
value, index := it.MinIndex(empty)
|
||||
// value: 0, index: -1
|
||||
```
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
name: MinIndexBy
|
||||
slug: minindexby
|
||||
sourceRef: it/find.go#L258
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func MinIndexBy[T any](collection iter.Seq[T], comparison func(a, b T) bool) (T, int)"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#minindexby
|
||||
similarHelpers:
|
||||
- core#slice#minindexby
|
||||
position: 470
|
||||
---
|
||||
|
||||
Searches the minimum value of a collection using a comparison function and returns both the value and its index.
|
||||
|
||||
If several values are equal to the smallest value, returns the first such value.
|
||||
Returns (zero value, -1) when the collection is empty.
|
||||
Will iterate through the entire sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Find the minimum string by length and its index
|
||||
words := it.Slice([]string{"apple", "hi", "banana", "ok"})
|
||||
value, index := it.MinIndexBy(words, func(a, b string) bool {
|
||||
return len(a) < len(b)
|
||||
})
|
||||
// value: "hi", index: 1
|
||||
|
||||
// Find the minimum person by age and its index
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 35},
|
||||
})
|
||||
value, index := it.MinIndexBy(people, func(a, b Person) bool {
|
||||
return a.Age < b.Age
|
||||
})
|
||||
// value: {Name: "Bob", Age: 25}, index: 1
|
||||
```
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
name: Mode
|
||||
slug: mode
|
||||
sourceRef: it/math.go#L124
|
||||
category: it
|
||||
subCategory: math
|
||||
signatures:
|
||||
- "func Mode[T constraints.Integer | constraints.Float](collection iter.Seq[T]) []T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#math#mode
|
||||
similarHelpers:
|
||||
- core#slice#mode
|
||||
position: 40
|
||||
---
|
||||
|
||||
Returns the mode (most frequent value) of a collection. If multiple values have the same highest frequency, then multiple values are returned. If the collection is empty, then the zero value of T is returned.
|
||||
|
||||
Will iterate through the entire sequence and allocate a map large enough to hold all distinct elements. Long heterogeneous input sequences can cause excessive memory usage.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(3)
|
||||
_ = yield(3)
|
||||
}
|
||||
mode := it.Mode(seq)
|
||||
// mode == []int{3}
|
||||
```
|
||||
|
||||
```go
|
||||
// Multiple modes
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("a")
|
||||
_ = yield("b")
|
||||
_ = yield("a")
|
||||
_ = yield("b")
|
||||
}
|
||||
mode := it.Mode(seq)
|
||||
// mode contains both "a" and "b" (order may vary)
|
||||
```
|
||||
@@ -0,0 +1,93 @@
|
||||
---
|
||||
name: None
|
||||
slug: none
|
||||
sourceRef: it/intersect.go#L63
|
||||
category: it
|
||||
subCategory: intersect
|
||||
signatures:
|
||||
- "func None[T comparable](collection iter.Seq[T], subset ...T) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#intersect#none
|
||||
similarHelpers:
|
||||
- core#slice#none
|
||||
position: 680
|
||||
---
|
||||
|
||||
Returns true if no element of a subset is contained in a collection or if the subset is empty.
|
||||
|
||||
Will iterate through the entire sequence if subset elements never match.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Check if collection contains none of the forbidden values
|
||||
numbers := it.Slice([]int{1, 3, 5, 7, 9})
|
||||
forbidden := []int{2, 4, 6, 8}
|
||||
hasNone := it.None(numbers, forbidden...)
|
||||
// hasNone: true
|
||||
|
||||
numbers = it.Slice([]int{1, 3, 5, 8, 9})
|
||||
hasNone = it.None(numbers, forbidden...)
|
||||
// hasNone: false (8 is in both collection and forbidden)
|
||||
|
||||
// Check if collection contains none of unwanted words
|
||||
words := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
unwanted := []string{"bad", "evil", "wrong"}
|
||||
hasNone := it.None(words, unwanted...)
|
||||
// hasNone: true
|
||||
|
||||
words = it.Slice([]string{"hello", "bad", "go", "lang"})
|
||||
hasNone = it.None(words, unwanted...)
|
||||
// hasNone: false ("bad" is in both)
|
||||
|
||||
// Check if collection contains none of specific IDs
|
||||
ids := it.Slice([]int{101, 102, 103, 104})
|
||||
restrictedIds := []int{201, 202, 203}
|
||||
hasNone := it.None(ids, restrictedIds...)
|
||||
// hasNone: true
|
||||
|
||||
ids = it.Slice([]int{101, 102, 203, 104})
|
||||
hasNone = it.None(ids, restrictedIds...)
|
||||
// hasNone: false (203 is restricted)
|
||||
|
||||
// Check with empty subset (always returns true)
|
||||
numbers = it.Slice([]int{1, 3, 5, 7, 9})
|
||||
hasNone = it.None(numbers)
|
||||
// hasNone: true
|
||||
|
||||
// Check with strings containing specific characters
|
||||
words := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
forbiddenChars := []string{"@", "#", "$"}
|
||||
hasNone := it.None(words, forbiddenChars...)
|
||||
// hasNone: true
|
||||
|
||||
words = it.Slice([]string{"hello", "world", "go@"})
|
||||
hasNone = it.None(words, forbiddenChars...)
|
||||
// hasNone: false (contains "@")
|
||||
|
||||
// Check if collection has none of problematic status codes
|
||||
statusCodes := it.Slice([]int{200, 201, 204})
|
||||
errorCodes := []int{400, 401, 403, 404, 500}
|
||||
hasNone := it.None(statusCodes, errorCodes...)
|
||||
// hasNone: true
|
||||
|
||||
statusCodes = it.Slice([]int{200, 404, 204})
|
||||
hasNone = it.None(statusCodes, errorCodes...)
|
||||
// hasNone: false (contains 404)
|
||||
|
||||
// Check with empty collection (always returns true)
|
||||
empty := it.Slice([]int{})
|
||||
hasNone = it.None(empty, 1, 2, 3)
|
||||
// hasNone: true
|
||||
|
||||
// Check for none of forbidden usernames
|
||||
usernames := it.Slice([]string{"alice", "bob", "charlie"})
|
||||
forbiddenUsers := []string{"admin", "root", "system"}
|
||||
hasNone := it.None(usernames, forbiddenUsers...)
|
||||
// hasNone: true
|
||||
|
||||
usernames = it.Slice([]string{"alice", "admin", "charlie"})
|
||||
hasNone = it.None(usernames, forbiddenUsers...)
|
||||
// hasNone: false ("admin" is forbidden)
|
||||
```
|
||||
@@ -0,0 +1,111 @@
|
||||
---
|
||||
name: NoneBy
|
||||
slug: noneby
|
||||
sourceRef: it/intersect.go#L69
|
||||
category: it
|
||||
subCategory: intersect
|
||||
signatures:
|
||||
- "func NoneBy[T any](collection iter.Seq[T], predicate func(item T) bool) bool"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#intersect#noneby
|
||||
similarHelpers:
|
||||
- core#slice#noneby
|
||||
position: 690
|
||||
---
|
||||
|
||||
Returns true if the predicate returns true for none of the elements in the collection or if the collection is empty.
|
||||
|
||||
Will iterate through the entire sequence if predicate never returns true.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Check if collection has no even numbers
|
||||
numbers := it.Slice([]int{1, 3, 5, 7, 9})
|
||||
hasNoEvens := it.NoneBy(numbers, func(n int) bool { return n%2 == 0 })
|
||||
// hasNoEvens: true
|
||||
|
||||
numbers = it.Slice([]int{1, 3, 5, 8, 9})
|
||||
hasNoEvens = it.NoneBy(numbers, func(n int) bool { return n%2 == 0 })
|
||||
// hasNoEvens: false (8 is even)
|
||||
|
||||
// Check if collection has no strings with specific prefix
|
||||
words := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
hasNoGoPrefix := it.NoneBy(words, func(s string) bool { return strings.HasPrefix(s, "go") })
|
||||
// hasNoGoPrefix: false ("go" has go prefix)
|
||||
|
||||
hasNoPythonPrefix := it.NoneBy(words, func(s string) bool { return strings.HasPrefix(s, "python") })
|
||||
// hasNoPythonPrefix: true
|
||||
|
||||
// Check if collection has no minors
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 35},
|
||||
})
|
||||
hasNoMinors := it.NoneBy(people, func(p Person) bool { return p.Age < 18 })
|
||||
// hasNoMinors: true
|
||||
|
||||
withMinor := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 16}, // Minor
|
||||
{Name: "Charlie", Age: 35},
|
||||
})
|
||||
hasNoMinors = it.NoneBy(withMinor, func(p Person) bool { return p.Age < 18 })
|
||||
// hasNoMinors: false
|
||||
|
||||
// Check if collection has no negative numbers
|
||||
numbers = it.Slice([]int{1, 3, 5, 7, 9})
|
||||
hasNoNegatives := it.NoneBy(numbers, func(n int) bool { return n < 0 })
|
||||
// hasNoNegatives: true
|
||||
|
||||
numbers = it.Slice([]int{1, -3, 5, 7, 9})
|
||||
hasNoNegatives = it.NoneBy(numbers, func(n int) bool { return n < 0 })
|
||||
// hasNoNegatives: false (-3 is negative)
|
||||
|
||||
// Check if collection has no uppercase strings
|
||||
strings := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
hasNoUppercase := it.NoneBy(strings, func(s string) bool { return s != strings.ToLower(s) })
|
||||
// hasNoUppercase: true
|
||||
|
||||
strings = it.Slice([]string{"hello", "World", "go", "lang"}) // "World" has uppercase
|
||||
hasNoUppercase = it.NoneBy(strings, func(s string) bool { return s != strings.ToLower(s) })
|
||||
// hasNoUppercase: false
|
||||
|
||||
// Empty collection returns true
|
||||
empty := it.Slice([]int{})
|
||||
hasNoEvens := it.NoneBy(empty, func(n int) bool { return n%2 == 0 })
|
||||
// hasNoEvens: true
|
||||
|
||||
// Check if collection has no invalid emails
|
||||
emails := it.Slice([]string{"user@example.com", "test@domain.org", "admin@site.net"})
|
||||
hasNoInvalid := it.NoneBy(emails, func(email string) bool {
|
||||
return !strings.Contains(email, "@") || !strings.Contains(email, ".")
|
||||
})
|
||||
// hasNoInvalid: true
|
||||
|
||||
emails = it.Slice([]string{"user@example.com", "invalid-email", "test@domain.org"})
|
||||
hasNoInvalid = it.NoneBy(emails, func(email string) bool {
|
||||
return !strings.Contains(email, "@") || !strings.Contains(email, ".")
|
||||
})
|
||||
// hasNoInvalid: false
|
||||
|
||||
// Check if collection has no numbers greater than 100
|
||||
numbers = it.Slice([]int{1, 3, 5, 7, 9})
|
||||
hasNoLargeNumbers := it.NoneBy(numbers, func(n int) bool { return n > 100 })
|
||||
// hasNoLargeNumbers: true
|
||||
|
||||
numbers = it.Slice([]int{1, 3, 5, 150, 9})
|
||||
hasNoLargeNumbers = it.NoneBy(numbers, func(n int) bool { return n > 100 })
|
||||
// hasNoLargeNumbers: false (150 > 100)
|
||||
|
||||
// Check if collection has no strings shorter than 3 characters
|
||||
words := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
hasNoShortWords := it.NoneBy(words, func(s string) bool { return len(s) < 3 })
|
||||
// hasNoShortWords: false ("go" has length 2)
|
||||
```
|
||||
@@ -0,0 +1,67 @@
|
||||
---
|
||||
name: Nth
|
||||
slug: nth
|
||||
sourceRef: it/find.go#L417
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func Nth[T any, N constraints.Integer](collection iter.Seq[T], nth N) (T, error)"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#nth
|
||||
similarHelpers:
|
||||
- core#slice#nth
|
||||
position: 580
|
||||
---
|
||||
|
||||
Returns the element at index `nth` of collection. Returns an error when nth is out of bounds.
|
||||
|
||||
Will iterate n times through the sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Get element at specific index
|
||||
numbers := it.Slice([]int{5, 2, 8, 1, 9})
|
||||
element, err := it.Nth(numbers, 2)
|
||||
// element: 8, err: nil
|
||||
|
||||
// Get first element (index 0)
|
||||
first, err := it.Nth(numbers, 0)
|
||||
// first: 5, err: nil
|
||||
|
||||
// Get last element
|
||||
last, err := it.Nth(numbers, 4)
|
||||
// last: 9, err: nil
|
||||
|
||||
// Out of bounds - negative
|
||||
_, err := it.Nth(numbers, -1)
|
||||
// err: nth: -1 out of bounds
|
||||
|
||||
// Out of bounds - too large
|
||||
_, err := it.Nth(numbers, 10)
|
||||
// err: nth: 10 out of bounds
|
||||
|
||||
// With strings
|
||||
words := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
element, err := it.Nth(words, 1)
|
||||
// element: "world", err: nil
|
||||
|
||||
// With different integer types
|
||||
numbers := it.Slice([]int{1, 2, 3, 4, 5})
|
||||
element, err := it.Nth(numbers, int8(3))
|
||||
// element: 4, err: nil
|
||||
|
||||
// With structs
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 35},
|
||||
})
|
||||
element, err := it.Nth(people, 1)
|
||||
// element: {Name: "Bob", Age: 25}, err: nil
|
||||
```
|
||||
@@ -0,0 +1,75 @@
|
||||
---
|
||||
name: NthOr
|
||||
slug: nthor
|
||||
sourceRef: it/find.go#L433
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func NthOr[T any, N constraints.Integer](collection iter.Seq[T], nth N, fallback T) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#nthor
|
||||
similarHelpers:
|
||||
- core#slice#nthor
|
||||
position: 590
|
||||
---
|
||||
|
||||
Returns the element at index `nth` of collection. If `nth` is out of bounds, returns the fallback value instead of an error.
|
||||
|
||||
Will iterate n times through the sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Get element at specific index
|
||||
numbers := it.Slice([]int{5, 2, 8, 1, 9})
|
||||
element := it.NthOr(numbers, 2, 42)
|
||||
// element: 8
|
||||
|
||||
// Get first element (index 0)
|
||||
first := it.NthOr(numbers, 0, 42)
|
||||
// first: 5
|
||||
|
||||
// Get last element
|
||||
last := it.NthOr(numbers, 4, 42)
|
||||
// last: 9
|
||||
|
||||
// Out of bounds - negative, returns fallback
|
||||
element := it.NthOr(numbers, -1, 42)
|
||||
// element: 42 (fallback)
|
||||
|
||||
// Out of bounds - too large, returns fallback
|
||||
element := it.NthOr(numbers, 10, 42)
|
||||
// element: 42 (fallback)
|
||||
|
||||
// With strings
|
||||
words := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
element := it.NthOr(words, 1, "fallback")
|
||||
// element: "world"
|
||||
|
||||
// Out of bounds with string fallback
|
||||
element := it.NthOr(words, 10, "fallback")
|
||||
// element: "fallback"
|
||||
|
||||
// With structs
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
})
|
||||
fallback := Person{Name: "Default", Age: 0}
|
||||
element := it.NthOr(people, 1, fallback)
|
||||
// element: {Name: "Bob", Age: 25}
|
||||
|
||||
// Out of bounds with struct fallback
|
||||
element := it.NthOr(people, 5, fallback)
|
||||
// element: {Name: "Default", Age: 0}
|
||||
|
||||
// With different integer types
|
||||
numbers := it.Slice([]int{1, 2, 3, 4, 5})
|
||||
element := it.NthOr(numbers, int8(3), 99)
|
||||
// element: 4
|
||||
```
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
name: NthOrEmpty
|
||||
slug: nthorempty
|
||||
sourceRef: it/find.go#L444
|
||||
category: it
|
||||
subCategory: find
|
||||
signatures:
|
||||
- "func NthOrEmpty[T any, N constraints.Integer](collection iter.Seq[T], nth N) T"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#find#nthorempty
|
||||
similarHelpers:
|
||||
- core#slice#nthorempty
|
||||
position: 600
|
||||
---
|
||||
|
||||
Returns the element at index `nth` of collection. If `nth` is out of bounds, returns the zero value (empty value) for that type.
|
||||
|
||||
Will iterate n times through the sequence.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
// Get element at specific index
|
||||
numbers := it.Slice([]int{5, 2, 8, 1, 9})
|
||||
element := it.NthOrEmpty(numbers, 2)
|
||||
// element: 8
|
||||
|
||||
// Get first element (index 0)
|
||||
first := it.NthOrEmpty(numbers, 0)
|
||||
// first: 5
|
||||
|
||||
// Get last element
|
||||
last := it.NthOrEmpty(numbers, 4)
|
||||
// last: 9
|
||||
|
||||
// Out of bounds - negative, returns zero value
|
||||
element := it.NthOrEmpty(numbers, -1)
|
||||
// element: 0 (zero value for int)
|
||||
|
||||
// Out of bounds - too large, returns zero value
|
||||
element := it.NthOrEmpty(numbers, 10)
|
||||
// element: 0 (zero value for int)
|
||||
|
||||
// With strings
|
||||
words := it.Slice([]string{"hello", "world", "go", "lang"})
|
||||
element := it.NthOrEmpty(words, 1)
|
||||
// element: "world"
|
||||
|
||||
// Out of bounds with string - returns empty string
|
||||
element := it.NthOrEmpty(words, 10)
|
||||
// element: "" (zero value for string)
|
||||
|
||||
// With structs
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
people := it.Slice([]Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
})
|
||||
element := it.NthOrEmpty(people, 1)
|
||||
// element: {Name: "Bob", Age: 25}
|
||||
|
||||
// Out of bounds with struct - returns zero value
|
||||
element := it.NthOrEmpty(people, 5)
|
||||
// element: {Name: "", Age: 0} (zero value for Person)
|
||||
|
||||
// With pointers - returns nil when out of bounds
|
||||
values := it.Slice([]*string{ptr("hello"), ptr("world")})
|
||||
element := it.NthOrEmpty(values, 1)
|
||||
// element: pointer to "world"
|
||||
|
||||
// Out of bounds with pointer - returns nil
|
||||
element := it.NthOrEmpty(values, 5)
|
||||
// element: nil (zero value for *string)
|
||||
|
||||
// With different integer types
|
||||
numbers := it.Slice([]int{1, 2, 3, 4, 5})
|
||||
element := it.NthOrEmpty(numbers, int8(3))
|
||||
// element: 4
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
name: PartitionBy
|
||||
slug: partitionby
|
||||
sourceRef: it/seq.go#L26
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func PartitionBy[T any, K comparable](collection iter.Seq[T], transform func(item T) K) [][]T"
|
||||
variantHelpers: []
|
||||
similarHelpers:
|
||||
- core#slice#partitionby
|
||||
position: 171
|
||||
---
|
||||
|
||||
PartitionBy returns a sequence of elements split into groups. The order of grouped values is
|
||||
determined by the order they occur in collection. The grouping is generated from the results
|
||||
of running each element of collection through transform.
|
||||
|
||||
```go
|
||||
collection := func(yield func(int) bool) {
|
||||
yield(1)
|
||||
yield(2)
|
||||
yield(3)
|
||||
yield(4)
|
||||
yield(5)
|
||||
yield(6)
|
||||
}
|
||||
|
||||
result := it.PartitionBy(collection, func(x int) int {
|
||||
return x % 3
|
||||
})
|
||||
// result contains [[1, 4], [2, 5], [3, 6]]
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
name: Product / ProductBy
|
||||
slug: product
|
||||
sourceRef: it/math.go#L70
|
||||
category: it
|
||||
subCategory: math
|
||||
signatures:
|
||||
- "func Product[T constraints.Float | constraints.Integer | constraints.Complex](collection iter.Seq[T]) T"
|
||||
- "func ProductBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection iter.Seq[T], iteratee func(item T) R) R"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#math#product
|
||||
- it#math#productby
|
||||
similarHelpers:
|
||||
- core#slice#product
|
||||
- core#slice#productby
|
||||
position: 20
|
||||
---
|
||||
|
||||
Multiplies values from a sequence. `ProductBy` applies a transform then multiplies. Returns 1 for empty sequences.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := it.RangeFrom(1, 4) // 1,2,3,4
|
||||
p := it.Product(seq)
|
||||
// p == 24
|
||||
```
|
||||
|
||||
```go
|
||||
nums := it.RangeFrom(2, 3) // 2,3,4
|
||||
p := it.ProductBy(nums, func(n int) int { return n - 1 })
|
||||
// (1*2*3) == 6
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
name: Range
|
||||
slug: range
|
||||
sourceRef: it/math.go#L12
|
||||
category: it
|
||||
subCategory: math
|
||||
signatures:
|
||||
- "func Range(elementNum int) iter.Seq[int]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#math#range
|
||||
similarHelpers:
|
||||
- core#slice#range
|
||||
- it#math#rangefrom
|
||||
- it#math#rangewithsteps
|
||||
position: 0
|
||||
---
|
||||
|
||||
Creates a sequence of integers starting from 0. Yields `elementNum` integers, stepping by ±1 depending on sign.
|
||||
|
||||
```go
|
||||
seq := it.Range(4)
|
||||
var out []int
|
||||
for v := range seq { out = append(out, v) }
|
||||
// out == []int{0, 1, 2, 3}
|
||||
```
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
name: RangeFrom
|
||||
slug: rangefrom
|
||||
sourceRef: it/math.go#L25
|
||||
category: it
|
||||
subCategory: math
|
||||
signatures:
|
||||
- "func RangeFrom[T constraints.Integer | constraints.Float](start T, elementNum int) iter.Seq[T]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#math#rangefrom
|
||||
similarHelpers:
|
||||
- core#slice#rangefrom
|
||||
- it#math#range
|
||||
- it#math#rangewithsteps
|
||||
position: 10
|
||||
---
|
||||
|
||||
Creates a sequence of numbers from start with specified length. Yields `elementNum` values starting from `start`, stepping by ±1 depending on sign.
|
||||
|
||||
```go
|
||||
seq := it.RangeFrom(5, 4)
|
||||
var result []int
|
||||
for item := range seq {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [5, 6, 7, 8]
|
||||
|
||||
seq2 := it.RangeFrom(10.5, 3)
|
||||
var result2 []float64
|
||||
for item := range seq2 {
|
||||
result2 = append(result2, item)
|
||||
}
|
||||
// result2 contains [10.5, 11.5, 12.5]
|
||||
```
|
||||
@@ -0,0 +1,42 @@
|
||||
---
|
||||
name: RangeWithSteps
|
||||
slug: rangewithsteps
|
||||
sourceRef: it/math.go#L35
|
||||
category: it
|
||||
subCategory: math
|
||||
signatures:
|
||||
- "func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step T) iter.Seq[T]"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#math#rangewithsteps
|
||||
similarHelpers:
|
||||
- core#slice#rangewithsteps
|
||||
- it#math#range
|
||||
- it#math#rangefrom
|
||||
position: 20
|
||||
---
|
||||
|
||||
Creates a sequence of numbers from start up to (excluding) end with a custom step. Step set to zero will return an empty sequence.
|
||||
|
||||
```go
|
||||
seq := it.RangeWithSteps(0, 10, 3)
|
||||
var result []int
|
||||
for item := range seq {
|
||||
result = append(result, item)
|
||||
}
|
||||
// result contains [0, 3, 6, 9]
|
||||
|
||||
seq2 := it.RangeWithSteps(10, 1, -3)
|
||||
var result2 []int
|
||||
for item := range seq2 {
|
||||
result2 = append(result2, item)
|
||||
}
|
||||
// result2 contains [10, 7, 4, 1]
|
||||
|
||||
seq3 := it.RangeWithSteps(0, 5, 1.5)
|
||||
var result3 []float64
|
||||
for item := range seq3 {
|
||||
result3 = append(result3, item)
|
||||
}
|
||||
// result3 contains [0, 1.5, 3, 4.5]
|
||||
```
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
name: Reduce
|
||||
slug: reduce
|
||||
sourceRef: it/seq.go#L133
|
||||
category: it
|
||||
subCategory: sequence
|
||||
signatures:
|
||||
- "func Reduce[T, R any](collection iter.Seq[T], accumulator func(agg R, item T) R, initial R) R"
|
||||
- "func ReduceLast[T, R any](collection iter.Seq[T], accumulator func(agg R, item T) R, initial R) R"
|
||||
- "func ReduceLastI[T, R any](collection iter.Seq[T], accumulator func(agg R, item T, index int) R, initial R) R"
|
||||
playUrl: ""
|
||||
variantHelpers:
|
||||
- it#sequence#reduce
|
||||
- it#sequence#reducei
|
||||
- it#sequence#reducelast
|
||||
- it#sequence#reducelasti
|
||||
similarHelpers:
|
||||
- core#slice#reduce
|
||||
- core#slice#reduceright
|
||||
position: 30
|
||||
---
|
||||
|
||||
Reduces a collection to a single accumulated value by applying an accumulator function to each element starting with an initial value.
|
||||
|
||||
Examples:
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
}
|
||||
sum := it.Reduce(seq, func(acc int, item int) int {
|
||||
return acc + item
|
||||
}, 0)
|
||||
// sum == 10
|
||||
```
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("hello")
|
||||
_ = yield("world")
|
||||
}
|
||||
concat := it.Reduce(seq, func(acc string, item string) string {
|
||||
return acc + " " + item
|
||||
}, "")
|
||||
// concat == " hello world"
|
||||
```
|
||||
|
||||
ReduceLast is like Reduce except that it iterates over elements of collection in reverse.
|
||||
|
||||
```go
|
||||
seq := func(yield func(int) bool) {
|
||||
_ = yield(1)
|
||||
_ = yield(2)
|
||||
_ = yield(3)
|
||||
_ = yield(4)
|
||||
}
|
||||
sum := it.ReduceLast(seq, func(acc int, item int) int {
|
||||
return acc + item
|
||||
}, 0)
|
||||
// sum == 10 (4 + 3 + 2 + 1 + 0)
|
||||
|
||||
result := it.ReduceLast(seq, func(agg string, item int) string {
|
||||
return fmt.Sprintf("%d-%s", item, agg)
|
||||
}, "end")
|
||||
// result == "4-3-2-1-end"
|
||||
```
|
||||
|
||||
ReduceLastI is like Reduce except that it iterates over elements of collection in reverse, with index.
|
||||
|
||||
```go
|
||||
seq := func(yield func(string) bool) {
|
||||
_ = yield("a")
|
||||
_ = yield("b")
|
||||
_ = yield("c")
|
||||
}
|
||||
result := it.ReduceLastI(seq, func(agg string, item string, index int) string {
|
||||
return fmt.Sprintf("%s:%d:%s", agg, index, item)
|
||||
}, "start")
|
||||
// result == "start:2:c:1:b:0:a"
|
||||
```
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user