ro

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README ΒΆ

ro

Go Version Release GoDoc Lint Test Coverage License

alexandreLamarre/ro (short for "range-over") is a generic iterator Go library based on Go 1.22+ rangefunc experiment.

The library is built for composability and readibility while providing similar functionality to other language's interator libraries such as itertools

This library is intended to be paired with samber/lo, where applicable.

πŸš€ Install

go get github.com/alexandreLamarre/ro@v0.1.0

βœ”οΈ Requirements

This library requires you to use go 1.23.X or greater.

πŸ’‘ Usage

You can import ro using:

import (
    "github.com/alexandreLamarre/ro"
)

πŸ‘€ Example

it := ro.Drop(
    ro.Apply(
        ro.Limit(
            ro.Permutations(
                ro.ToSlice(
                    ro.Range(0, 6, 1),
                ),
                5,
            ),
            5,
        ),
        func(perm []int) int {
            return digitsToInt(perm)
        },
    ),
    func(i int) bool {
        return i > 10000
    },
)
for v := range it {
    fmt.Println(v)
}

yields the following output:

1234
2134

The above iterator yields all numbers < 10000 that are formed from the digits of the first 5 generated permutations of {0,1,2,3,4,5} of size 5

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func Accumulate ΒΆ

func Accumulate[T intType](seq iter.Seq[T]) iter.Seq[T]

Accumulate returns an iterator that yields the accumulated sum of the elements yielded by seq

func AccumulateFunc ΒΆ

func AccumulateFunc[T any](seq iter.Seq[T], f func(T, T) T) iter.Seq[T]

AccumulateFunc returns an iterator that yields the accumulated result of applying f to the elements yielded by seq

func AccumulateFuncSlice ΒΆ

func AccumulateFuncSlice[T any](arr []T, f func(T, T) T) iter.Seq[T]

AccumulateFuncSlice returns an iterator that yields the accumulated result of applying f to the elements in the slice

func AccumulateSlice ΒΆ

func AccumulateSlice[T intType](arr []T) iter.Seq[T]

AccumulateSlice returns an iterator that yields the accumulated sum of the elements in the slice

func Apply ΒΆ

func Apply[U, V any](seq iter.Seq[U], f func(U) V) iter.Seq[V]

Apply returns an iterator that yields the result of applying f to each element yielded by the sequence

func ApplySlice ΒΆ

func ApplySlice[U, V any](arr []U, f func(U) V) iter.Seq[V]

ApplySlice returns an iterator that yields the result of applying f to each element in the slice

func Chain ΒΆ

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

Chain returns an iterator that yields the elements yielded by seqs in order

func ChainSlice ΒΆ

func ChainSlice[T any](arr ...[]T) iter.Seq[T]

ChainSlice returns an iterator that yields the elements of the slice in order

func Combinations ΒΆ

func Combinations[T any](arr []T, k int) iter.Seq[[]T]

Combinations returns an iterator that yields all possible k-length combinations of the slice If k <= 0, the empty iterator is returned. If k > len(seq), the iterator yields all combinations of seq

func Count ΒΆ

func Count[T intType](start, step T) iter.Seq[T]

Count returns an infinite iterator starting from start and incrementing by step

func Cycle ΒΆ

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

Cycle returns an infinite iterator that cycles repeatedly through the elements of sequence

func CycleSlice ΒΆ

func CycleSlice[T any](arr []T) iter.Seq[T]

CycleSlice returns an infinite iterator that cycles repeatedly through the elements of the slice

func Drop ΒΆ

func Drop[T any](seq iter.Seq[T], predicate func(T) bool) iter.Seq[T]

Drop returns an iterator that yields elements not matching the predicate

func DropSlice ΒΆ

func DropSlice[T any](arr []T, predicate func(T) bool) iter.Seq[T]

DropSlice returns an iterator that yields elements not matching the predicate

func Extend ΒΆ

func Extend[T any](seq iter.Seq[T]) iter.Seq2[struct{}, T]

Extend pads an iterator with an empty struct to conform to an iter.Seq2 type

func Filter ΒΆ

func Filter[T any](seq iter.Seq[T], predicate func(T) bool) iter.Seq[T]

Filter returns an iterator that yields elements matching the predicate

func FilterSlice ΒΆ

func FilterSlice[T any](arr []T, predicate func(T) bool) iter.Seq[T]

FilterSlice returns an iterator that yields elements matching the predicate

func FromSlice ΒΆ

func FromSlice[T any](arr []T) iter.Seq[T]

FromSlice is a convenience wrapper to convert a slice to an iterator

func FromString ΒΆ

func FromString(s string) iter.Seq[rune]

FromString is a convenience wrapper to convert a string to an iterator

func Index ΒΆ

func Index[T any](seq iter.Seq[T]) iter.Seq2[int, T]

Index returns an iterator that yields the index and value of the yielded elements from the sequence

func Limit ΒΆ

func Limit[T any](seq iter.Seq[T], n int) iter.Seq[T]

Limit returns an iterator that yields the up to the first n elements yielded by the sequence

func LimitSlice ΒΆ

func LimitSlice[T any](arr []T, n int) iter.Seq[T]

LimitSlice returns an iterator that yields the up to the first n elements of the slice

func PairWise ΒΆ

func PairWise[T any](seq iter.Seq[T]) iter.Seq[[2]T]

PairWise returns an iterator that yields pairs of adjacent elements yielded by seq If the sequence has less than 2 elements, the empty iterator is returned

func PairWiseSlice ΒΆ

func PairWiseSlice[T any](arr []T) iter.Seq[[2]T]

PairwiseSlice returns an iterator that yields pairs of adjacent elements in the slice If the slice has less than 2 elements, the empty iterator is returned

func Permutations ΒΆ

func Permutations[T any](arr []T, k int) iter.Seq[[]T]

Permutations returns an iterator that yields all possible n-length permutations of the slice If k <= 0, the empty iterator is returned. If k > len(seq), the iterator yields all permutations of seq

Note : allocates a state buffer of the same size as the input to keep track of visited permutations as we iterate through them.

func Product ΒΆ

func Product[U, V any](seq1 iter.Seq[U], seq2 iter.Seq[V]) iter.Seq[lo.Tuple2[U, V]]

Product returns an iterator that yields the cartesian product of seq1 and seq2 as a tuple. The returned iterator of tuples is ordered [A1, B1], [A1, B2], [A2, B1], [A2, B2], ...

Expects to fully consume seq1 before seq2

func ProductSlice ΒΆ

func ProductSlice[U, V any](arr1 []U, arr2 []V) iter.Seq[lo.Tuple2[U, V]]

ProductSlice returns an iterator that yields the cartesian product of slice1 and slice2 as a tuple. The returned iterator of tuples is ordered [A1, B1], [A1, B2], [A2, B1], [A2, B2], ...

func Range ΒΆ

func Range[T intType](start, end, step T) iter.Seq[T]

Range returns an iterator that yields integers from start to end (exclusive) by step

func RangeOver ΒΆ

func RangeOver[T any](seq iter.Seq[T], start, step, stop int) iter.Seq[T]

RangeOver returns an iterator that yields the elements of the sequence from start to stop incrementing by step

func Repeat ΒΆ

func Repeat[T intType](elem T) iter.Seq[T]

Repeat returns an infinite iterator that yields elem indefinitely

func Tee ΒΆ

func Tee[T any](seq iter.Seq[T], n int) []iter.Seq[T]

Tee returns n iterators that yield the elements of the sequence If n == 1, the only element in the slice will be the original seq

func ToSlice ΒΆ

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

ToSlice is a convenience wrapper to convert an iterator to a slice

This will block until the iterator is exhausted.

func Unpack ΒΆ

func Unpack[U, V any](seq iter.Seq2[U, V]) (iter.Seq[U], iter.Seq[V])

Unpack returns two iterators that yield the keys and values of the sequence

func UnpackMap ΒΆ

func UnpackMap[U comparable, V any](in map[U]V) (iter.Seq[U], iter.Seq[V])

UnpackMap returns two iterators that yield the keys and values of the map

func While ΒΆ

func While[T any](seq iter.Seq[T], predicate func(T) bool) iter.Seq[T]

While returns an iterator that yields elements of the sequence until the predicate is false

func WhileSlice ΒΆ

func WhileSlice[T any](arr []T, predicate func(T) bool) iter.Seq[T]

WhileSlice returns an iterator that yields elements of the slice until the predicate is false

func Zip ΒΆ

func Zip[U, V any](seq1 iter.Seq[U], seq2 iter.Seq[V]) iter.Seq[lo.Tuple2[U, V]]

Zip returns an iterator that yields the elements of seq1 and seq2 as a tuple

func ZipFill ΒΆ

func ZipFill[U, V any](seq1 iter.Seq[U], seq2 iter.Seq[V], fillU U, fillV V) iter.Seq[lo.Tuple2[U, V]]

ZipFill returns an iterator that yields the elements of seq1 and seq2 as a tuple, padding missing values as necessary

func ZipFillSlice ΒΆ

func ZipFillSlice[U, V any](arr1 []U, arr2 []V, fillU U, fillV V) iter.Seq[lo.Tuple2[U, V]]

ZipFillSlice returns an iterator that yields the elements of arr1 and arr2 as a tuple, padding missing values as necessary

func ZipSlice ΒΆ

func ZipSlice[U, V any](arr1 []U, arr2 []V) iter.Seq[lo.Tuple2[U, V]]

ZipSlice returns an iterator that yields the elements of arr1 and arr2 as a tuple

Types ΒΆ

This section is empty.

Jump to

Keyboard shortcuts

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