$ go get github.com/kafkiansky/itertools
The itertools
package consists from two subpackages: iterslice
and itermap
for working with slices and maps as iterators separately.
Creates an iterator from slice.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
for v := range iterslice.Iterator([]int{1, 2, 3}) {
log.Println(v)
}
}
Filters an iter.Seq[T]
.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
for v := range iterslice.Filter(
iterslice.Iterator([]int{1, 2, 3}),
func(v int) bool {
return v%2 == 0
},
) {
log.Println(v)
}
}
Apply F
to an iter.Seq[T]
returning new iter.Seq[E]
.
package main
import (
"log"
"strconv"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
for v := range iterslice.Map(
iterslice.Iterator([]int{1, 2, 3}),
strconv.Itoa,
) {
log.Println(v)
}
}
Works as Map
but with errors.
package main
import (
"log"
"strconv"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
for v, err := range iterslice.TryMap(
iterslice.Iterator([]string{"1", "2", "invalid"}),
strconv.Atoi,
) {
if err != nil {
log.Println(err)
} else {
log.Println(v)
}
}
}
Iterates over an iter.Seq[T]
and applies F
.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
iterslice.Each(
iterslice.Iterator([]int{1, 2, 3}),
func(v int) {
log.Println(v)
},
)
}
Yields the first element from an iter.Seq[T]
.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
log.Println(
iterslice.First(
iterslice.Iterator([]int{6, 7, 8}),
),
) // 6, true
}
Yield the last element from an iter.Seq[T]
.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
log.Println(
iterslice.Last(
iterslice.Iterator([]int{6, 7, 8}),
),
) // 8, true
}
Yields the N
element from iter.Seq[T]
. Index starts from 1.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
log.Println(
iterslice.Nth(
iterslice.Iterator([]int{6, 7, 8}),
2,
),
) // 7, true
}
Creates an iterator from sequence.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
for v := range iterslice.Range(5) {
log.Println(v)
}
}
Creates an iterator starting from left value to rigth exclusive.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
for v := range iterslice.Between(5, 10) {
log.Println(v)
}
}
Reduces an iter.Seq[T]
to a single value using an F
. Optional accepts an initial value.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
log.Println(
iterslice.Reduce(
iterslice.Iterator([]int{1, 2, 3}),
func(a, b int) int { return a + b },
),
) // 6
}
Splits string using separator and yield chunks as an iter.Seq[string]
.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
for v := range iterslice.Split("a,b,c", ",") {
log.Println(v)
}
}
Yield string chars as runes.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
for v := range iterslice.Chars("итератор") {
log.Println(v)
}
}
Consume values from an chan T
and yields values as iter.Seq[T]
until channel closes.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
c := make(chan int, 3)
for v := range 3 {
c <- v
}
close(c)
for v := range iterslice.ConsumeChannel(c) {
log.Println(v)
}
}
Creates two iterators from one using F
.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
even, odd := iterslice.Partition(
iterslice.Iterator([]int{1, 2, 3}),
func(v int) bool {
return v%2 == 0
},
)
for v := range even {
log.Printf("even: %d", v)
}
for v := range odd {
log.Printf("odd: %d", v)
}
}
Search index of value in an iterator.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
log.Println(
iterslice.Position(
iterslice.Iterator([]int{1, 2, 3}),
2,
),
) // 1 (index of value 2)
}
Search the max value in an iter.Seq[constraints.Order]
.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
log.Println(
iterslice.Max(
iterslice.Iterator([]int{10, 2, 3}),
),
)
}
Search the min value in an iter.Seq[constraints.Ordered]
.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
log.Println(
iterslice.Min(
iterslice.Iterator([]int{-10, -2, -3}),
),
) // -10
}
Joins n iterators to the single one.
package main
import (
"log"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
for v := range iterslice.Join(
iterslice.Between(0, 3),
iterslice.Between(6, 10),
) {
log.Println(v)
}
}
Creates an iterator iter.Seq2[K, V]
from map[K]V
.
package main
import (
"log"
"github.com/kafkiansky/itertools/itermap"
)
func main() {
for k, v := range itermap.Iterator(map[string]int{"x": 1, "y": 2}) {
log.Printf("key: %s, value: %d", k, v)
}
}
Creates an iterator iter.Seq[K]
from map keys.
package main
import (
"log"
"github.com/kafkiansky/itertools/itermap"
)
func main() {
for k := range itermap.Keys(
map[string]int{"x": 1, "y": 2},
) {
log.Printf("key: %s", k)
}
}
Creates an iterator iter.Seq[V]
from map values.
package main
import (
"log"
"github.com/kafkiansky/itertools/itermap"
)
func main() {
for v := range itermap.Values(
map[string]int{"x": 1, "y": 2},
) {
log.Printf("value: %d", v)
}
}
Iterates over an iter.Seq2[K, V]
an applies F
for each key-value pair.
package main
import (
"log"
"github.com/kafkiansky/itertools/itermap"
)
func main() {
itermap.Each(
itermap.Iterator(map[string]int{"x": 1, "y": 2}),
func(k string, v int) {
log.Printf("key: %s, value: %d", k, v)
},
)
}
Joins n iterators iter.Seq[K, V]
to the single one.
package main
import (
"log"
"github.com/kafkiansky/itertools/itermap"
)
func main() {
for k, v := range itermap.Join(
itermap.Iterator(map[string]int{"x": 1, "y": 2}),
itermap.Iterator(map[string]int{"z": 3}),
) {
log.Printf("key: %s, value: %d", k, v)
}
}
Collects an iter.Seq[T]
to the slice []T
.
package main
import (
"log"
"github.com/kafkiansky/itertools"
"github.com/kafkiansky/itertools/iterslice"
)
func main() {
log.Println(
itertools.CollectSlice(
iterslice.Between(5, 10),
),
) // [5, 6, 7, 8, 9]
}
Collects an iter.Seq2[K, V]
to the map map[K]V
.
package main
import (
"log"
"github.com/kafkiansky/itertools"
"github.com/kafkiansky/itertools/itermap"
)
func main() {
log.Println(
itertools.CollectMap(
itermap.Iterator(map[string]int{"x": 1, "y": 2}),
),
) // map[x:1 y:2]
}
$ GOEXPERIMENT=rangefunc go test ./...
The MIT License (MIT). See License File for more information.