iter

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2024 License: MIT Imports: 3 Imported by: 0

README

Iter

A go package that provides a ways to iterate over elements.

Installation

go get github.com/tommoulard/iter

Usage

package main

import (
    "fmt"

    "github.com/tommoulard/iter"
)


func main() {
    for a, b := range iter.Zip([]int{1, 2, 3}, []int{4, 5, 6}) {
        fmt.Println(a, b)
    }

    // Output:
    // 1 4
    // 2 5
    // 3 6
}

See the GoDoc for more information.

Ideas

Documentation

Overview

Package iter provides functions to work with sequences.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accumulate

func Accumulate[T cmp.Ordered](a []T) iter.Seq[T]

Accumulate returns a sequence of accumulated values. The first element is the same as the first element of the input sequence. The second element is the sum of the first and second elements of the input sequence. So on and so forth.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a := range iter.Accumulate([]int{1, 2, 3, 4, 5}) {
		fmt.Println(a)
	}

}
Output:

1
3
6
10
15

func Append added in v0.2.0

func Append[T any](seqs ...iter.Seq[T]) iter.Seq[T]

Append returns a sequence of elements from the concatenation of the input sequences.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	i1 := iter.Chain([]int{1, 2, 3})
	i2 := iter.Chain([]int{4, 5, 6})

	for value := range iter.Append(i1, i2) {
		fmt.Println(value)
	}

}
Output:

1
2
3
4
5
6

func Append2 added in v0.2.0

func Append2[T, U any](seqs ...iter.Seq2[T, U]) iter.Seq2[T, U]

Append2 returns a sequence of elements from the concatenation of the input sequences.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	i1 := iter.ChainMap(map[int]string{1: "1", 2: "2", 3: "3"})
	i2 := iter.ChainMap(map[int]string{4: "4", 5: "5", 6: "6"})

	for key, value := range iter.Append2(i1, i2) {
		fmt.Printf("%d: %q\n", key, value)
	}

}
Output:

1: "1"
2: "2"
3: "3"
4: "4"
5: "5"
6: "6"

func Chain

func Chain[T any](seqs ...[]T) iter.Seq[T]

Chain returns a sequence of elements from the input sequences. The resulting sequence is the concatenation of the input sequences.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a := range iter.Chain([]int{1, 2, 3}, []int{4, 5, 6}) {
		fmt.Println(a)
	}

}
Output:

1
2
3
4
5
6

func ChainMap

func ChainMap[T cmp.Ordered, U any](maps ...map[T]U) iter.Seq2[T, U]

ChainMap returns a sequence of elements from the input map. The resulting sequence is ordered by the keys of the input map.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	m := map[int]string{
		int('b'): "b",
		int('c'): "c",
		int('a'): "a",
	}
	for key, value := range iter.ChainMap(m) {
		fmt.Println(key, value)
	}

}
Output:

97 a
98 b
99 c

func ChainSeq

func ChainSeq[T any](seqs ...iter.Seq[T]) iter.Seq[T]

ChainSeq returns a sequence of elements from the input sequences. The resulting sequence is the concatenation of the input sequences.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/tommoulard/iter"
)

func main() {
	for a := range iter.ChainSeq(slices.Values([]int{1, 2, 3}), slices.Values([]int{4, 5, 6})) {
		fmt.Println(a)
	}

}
Output:

1
2
3
4
5
6

func Compress

func Compress[T any](data []T, selectors []bool) iter.Seq[T]

Compress returns a sequence of elements from the input sequence. The resulting sequence contains only the elements where the corresponding selector is true.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a := range iter.Compress([]int{1, 2, 3}, []bool{true, false, true}) {
		fmt.Println(a)
	}

}
Output:

1
3

func DropWhile

func DropWhile[T any](pred func(T) bool, a []T) iter.Seq[T]

DropWhile returns a sequence of elements from the input sequence. The resulting sequence contains only the elements after the predicate is false.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a := range iter.DropWhile(func(x int) bool { return x < 5 }, []int{1, 4, 6, 3, 8}) {
		fmt.Println(a)
	}

}
Output:

6
3
8

func Equal added in v0.2.0

func Equal[T comparable](seqA, seqB iter.Seq[T]) bool
Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	fmt.Println(iter.Equal(iter.Chain([]int{1, 2, 3}), iter.Chain([]int{1, 2, 3}))) // true
	fmt.Println(iter.Equal(iter.Chain([]int{1, 2, 3}), iter.Chain([]int{1, 2})))    // true
	fmt.Println(iter.Equal(iter.Chain([]int{1, 2, 5}), iter.Chain([]int{1, 2, 3}))) // false
	fmt.Println(iter.Equal(iter.Chain([]int{1, 2, 5}), iter.Chain([]int{1, 2})))    // true

}
Output:

true
true
false
true

func Filter

func Filter[T any](pred func(T) bool, a []T) iter.Seq[T]

Filter returns a sequence of elements from the input sequence. The resulting sequence contains only the elements where the predicate is true.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a := range iter.Filter(func(x int) bool { return x%2 == 0 }, []int{1, 2, 3, 4, 5}) {
		fmt.Println(a)
	}

}
Output:

2
4

func FilterFalse

func FilterFalse[T any](pred func(T) bool, a []T) iter.Seq[T]

FilterFalse returns a sequence of elements from the input sequence. The resulting sequence contains only the elements where the predicate is false.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a := range iter.FilterFalse(func(x int) bool { return x%2 == 0 }, []int{1, 2, 3, 4, 5}) {
		fmt.Println(a)
	}

}
Output:

1
3
5

func First added in v0.2.0

func First[T, U any](seq iter.Seq2[T, U]) iter.Seq[T]

First returns a sequence of elements from the input sequence. The resulting sequence contains only the first element of the input sequence.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	i := iter.ChainMap(map[int]string{1: "2", 2: "4", 3: "6"})

	for key := range iter.First(i) {
		fmt.Println(key)
	}

}
Output:

1
2
3

func GroupBy

func GroupBy[T any, K comparable](key func(T) K, a []T) iter.Seq2[K, iter.Seq[T]]

GroupBy returns a sequence of groups of elements from the input sequence. The resulting sequence contains groups of elements where the key function returns the same value.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	out := make(map[int][]int)
	for k, v := range iter.GroupBy(func(x int) int { return x % 2 }, []int{1, 2, 3, 4, 5}) {
		out[k] = iter.Values(v)
	}

	fmt.Println(out[0])
	fmt.Println(out[1])

}
Output:

[2 4]
[1 3 5]

func IZip added in v0.2.0

func IZip[T, U any](seqA iter.Seq[T], seqB iter.Seq[U]) iter.Seq2[T, U]

IZip returns a sequence of pairs of elements from the input sequences. The resulting sequence is as long as the shortest input sequence.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a, b := range iter.IZip(iter.Chain([]int{1, 2, 3}), iter.Chain([]int{4, 5, 6})) {
		fmt.Println(a, b)
	}

}
Output:

1 4
2 5
3 6

func Map

func Map[T, U any](f func(T) U, a []T) iter.Seq[U]

Map returns a sequence of elements from the input sequence. The resulting sequence contains the elements after applying the function to each element of the input sequence.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a := range iter.Map(func(x int) int { return x * 2 }, []int{1, 2, 3, 4, 5}) {
		fmt.Println(a)
	}

}
Output:

2
4
6
8
10

func Map2

func Map2[T, U, V any](f func(T, U) V, a []T, b []U) iter.Seq[V]

Map2 returns a sequence of elements from the input sequences. The resulting sequence contains the elements after applying the function to each pair of elements from the input sequences.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a := range iter.Map2(func(x, y int) int { return x + y }, []int{1, 2, 3}, []int{4, 5, 6}) {
		fmt.Println(a)
	}

}
Output:

5
7
9

func Permutations added in v0.2.0

func Permutations[T any](a []T) iter.Seq[[]T]

Permutations returns a sequence of permutations of the input sequence. The resulting sequence contains all possible permutations of the input sequence. This function is a helper for `PermutationsLen(a, len(a))`.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for value := range iter.Permutations([]int{1, 2, 3}) {
		fmt.Println(value)
	}

}
Output:

[1 2 3]
[2 1 3]
[3 1 2]
[1 3 2]
[2 3 1]
[3 2 1]

func PermutationsLen added in v0.2.0

func PermutationsLen[T any](a []T, length int) iter.Seq[[]T]

PermutationsLen returns a sequence of permutations of the input sequence. The resulting sequence contains all possible permutations of the input sequence with the specified length. It uses the Heap's algorithm to generate the permutations.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for value := range iter.PermutationsLen([]int{1, 2, 3}, 2) {
		fmt.Println(value)
	}

}
Output:

[1 2]
[2 1]
[3 1]
[1 3]
[2 3]
[3 2]

func Second added in v0.2.0

func Second[T, U any](seq iter.Seq2[T, U]) iter.Seq[U]

Second returns a sequence of elements from the input sequence. The resulting sequence contains only the second element of the input sequence.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	i := iter.ChainMap(map[int]string{1: "2", 2: "4", 3: "6"})

	for Values := range iter.Second(i) {
		fmt.Printf("%q\n", Values)
	}

}
Output:

"2"
"4"
"6"

func TakeWhile

func TakeWhile[T any](pred func(T) bool, a []T) iter.Seq[T]

TakeWhile returns a sequence of elements from the input sequence. The resulting sequence contains only the elements before the predicate is false.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a := range iter.TakeWhile(func(x int) bool { return x < 5 }, []int{1, 4, 6, 3, 8}) {
		fmt.Println(a)
	}

}
Output:

1
4

func Values

func Values[T any](seq iter.Seq[T]) []T

Values returns a slice of elements from the input sequence.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/tommoulard/iter"
)

func main() {
	intSeq := slices.Values([]int{1, 2, 3})
	fmt.Println(iter.Values(intSeq))

}
Output:

[1 2 3]

func Values2

func Values2[T, U any](seq iter.Seq2[T, U]) ([]T, []U)

Values2 returns a pair of slices of elements from the input sequence.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	intSeq2 := iter.Zip([]int{1, 2, 3}, []int{4, 5, 6})
	fmt.Println(iter.Values2(intSeq2))

}
Output:

[1 2 3] [4 5 6]

func Values2Map added in v0.2.0

func Values2Map[T comparable, U any](seq iter.Seq2[T, U]) map[T]U

Values2Map returns a map of elements from the input sequence.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	intSeq2 := iter.Zip([]int{1, 2, 3}, []int{4, 5, 6})
	fmt.Println(iter.Values2Map(intSeq2))

}
Output:

map[1:4 2:5 3:6]

func Zip

func Zip[T, U any](a []T, b []U) iter.Seq2[T, U]

Zip returns a sequence of pairs of elements from the input sequences. The resulting sequence is as long as the shortest input sequence.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a, b := range iter.Zip([]int{1, 2, 3}, []int{4, 5, 6}) {
		fmt.Println(a, b)
	}

}
Output:

1 4
2 5
3 6

func ZipLongest

func ZipLongest[T, U any](a []T, b []U, fill either[T, U]) iter.Seq2[T, U]

ZipLongest returns a sequence of pairs of elements from the input sequences. The resulting sequence is as long as the longest input sequence. If one sequence is shorter than the other, the missing values are filled with the provided fill value.

Example
package main

import (
	"fmt"

	"github.com/tommoulard/iter"
)

func main() {
	for a, b := range iter.ZipLongest([]int{1, 2, 3}, []int{4}, 0) {
		fmt.Println(a, b)
	}

}
Output:

1 4
2 0
3 0

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL