Documentation
¶
Index ¶
- func All[V any](s []V) iter.Seq[V]
- func Chain[V any](seqs ...iter.Seq[V]) iter.Seq[V]
- func Count[V any](seq iter.Seq[V]) int
- func Cycle[V any](seq iter.Seq[V]) iter.Seq[V]
- func Empty[V any]() iter.Seq[V]
- func Filter[V any](seq iter.Seq[V], predicate func(V) bool) iter.Seq[V]
- func Fold[V, R any](seq iter.Seq[V], init R, f func(R, V) R) R
- func Last[V any](seq iter.Seq[V]) (V, bool)
- func Map[V, R any](seq iter.Seq[V], f func(V) R) iter.Seq[R]
- func Max[V cmp.Ordered](seq iter.Seq[V]) (V, bool)
- func Min[V cmp.Ordered](seq iter.Seq[V]) (V, bool)
- func Nth[V any](seq iter.Seq[V], n int) (V, bool)
- func Repeat[V any](v V) iter.Seq[V]
- func Skip[V any](seq iter.Seq[V], n int) iter.Seq[V]
- func Take[V any](seq iter.Seq[V], n int) iter.Seq[V]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶ added in v0.1.0
All returns an iterator over all elements of s.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, 2, 3})
for v := range nums {
fmt.Println(v)
}
}
Output: 1 2 3
func Chain ¶ added in v0.1.0
Chain returns an iterator that yields elements from each of seqs in turn.
If seqs is empty, the returned iterator is also empty.
Example ¶
package main
import (
"fmt"
"iter"
"github.com/kyminbb/it"
)
func main() {
seqs := []iter.Seq[int]{it.All([]int{1, 2}), it.All([]int{3, 4})}
chain := it.Chain(seqs...)
for v := range chain {
fmt.Println(v)
}
}
Output: 1 2 3 4
func Count ¶ added in v0.1.0
Count iterates over seq and returns the number of elements.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, 2, 3})
fmt.Println(it.Count(nums))
}
Output: 3
func Cycle ¶ added in v0.1.0
Cycle returns an iterator that repeats seq endlessly.
If seq is empty, the returned iterator is also empty.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, 2, 3})
cycle := it.Cycle(nums)
i := 0
for v := range cycle {
if i == 6 {
break
}
fmt.Println(v)
i++
}
}
Output: 1 2 3 1 2 3
func Empty ¶ added in v0.1.0
Empty returns an iterator that yields nothing.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
empty := it.Empty[int]()
for range empty {
fmt.Println("This should not be printed")
}
}
func Filter ¶ added in v0.1.0
Filter returns an iterator that yields only the elements of seq for which predicate is true.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, -2, 3})
positives := it.Filter(nums, func(x int) bool { return x > 0 })
for v := range positives {
fmt.Println(v)
}
}
Output: 1 3
func Fold ¶ added in v0.1.0
Fold applies f to each element of seq and returns the accumulated result.
init is the initial value of the accumulator. f takes the accumulator and an element of seq as arguments. The result of each call to f becomes the accumulator for the next call.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, 2, 3})
sum := it.Fold(nums, 0, func(acc, v int) int { return acc + v })
fmt.Println(sum)
nums = it.All([]int{3, 4, 5})
product := it.Fold(nums, 1, func(acc, v int) int { return acc * v })
fmt.Println(product)
}
Output: 6 60
func Last ¶ added in v0.1.0
Last iterates over seq and returns the last element. The second return value reports whether the element exists (seq is not empty).
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, 2, 3})
fmt.Println(it.Last(nums))
nums = it.All([]int{})
fmt.Println(it.Last(nums))
}
Output: 3 true 0 false
func Map ¶ added in v0.1.0
Map returns an iterator that calls f on each element of seq.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, 2, 3})
doubles := it.Map(nums, func(x int) int { return x * 2 })
for v := range doubles {
fmt.Println(v)
}
}
Output: 2 4 6
func Max ¶ added in v0.1.0
Max returns the maximum element of seq. The second return value reports whether the element exists (seq is not empty).
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, 2, 3})
fmt.Println(it.Max(nums))
nums = it.All([]int{})
fmt.Println(it.Max(nums))
}
Output: 3 true 0 false
func Min ¶ added in v0.1.0
Min returns the minimum element of seq. The second return value reports whether the element exists (seq is not empty).
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{3, 2, 1})
fmt.Println(it.Min(nums))
nums = it.All([]int{})
fmt.Println(it.Min(nums))
}
Output: 1 true 0 false
func Nth ¶ added in v0.1.0
Nth returns the nth element of seq. The second return value reports whether the element exists.
The count is zero-based, so Nth(seq, 0) returns the first element.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, 2, 3})
fmt.Println(it.Nth(nums, 1))
fmt.Println(it.Nth(nums, 3))
}
Output: 2 true 0 false
func Repeat ¶ added in v0.1.0
Repeat returns an iterator that yields v endlessly.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
repeat := it.Repeat(3)
i := 0
for v := range repeat {
if i == 3 {
break
}
fmt.Println(v)
i++
}
}
Output: 3 3 3
func Skip ¶ added in v0.1.0
Skip returns an iterator that skips the first n elements of seq and yields the rest.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, 2, 3})
skip := it.Skip(nums, 2)
for v := range skip {
fmt.Println(v)
}
}
Output: 3
func Take ¶ added in v0.1.0
Take returns an iterator that yields the first n elements of seq, or fewer if seq ends sooner.
Example ¶
package main
import (
"fmt"
"github.com/kyminbb/it"
)
func main() {
nums := it.All([]int{1, 2, 3})
take := it.Take(nums, 2)
for v := range take {
fmt.Println(v)
}
}
Output: 1 2
Types ¶
This section is empty.