iter

package
v0.6.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.5.0

func All[E any](s Seq[E], f func(E) bool) bool

All tests if every element of the iterator matches a predicate.

Example:

iter.All(seq(1,2,3), func(x int) bool { return i > 0 }) => true
iter.Any(seq(1,2,3), func(x int) bool { return i > 2 }) => false

func Any added in v0.5.0

func Any[E any](s Seq[E], f func(E) bool) (ok bool)

Any tests if any element of the iterator matches a predicate.

Example:

iter.Any(seq(1,2,3), func(x int) bool { return i % 2 == 0 }) => true
iter.Any(seq(1,2,3), func(x int) bool { return i < 0 })      => false

func CollectFunc added in v0.6.0

func CollectFunc[E any](s Seq[E], collect func(E) bool)

CollectFunc call func on each element to collect it.

Stopping iterate when collect returns false.

collect return false means collector maybe full, or no need to collect more elements.

Example:

res := make(chan int, 2)
iter.CollectFunc(seq(1,2,3), func (x int) bool {
	select {
	case res <- x:
		return true
	default:
		return false
	}
})

func Contains added in v0.6.0

func Contains[E comparable](s Seq[E], x E) bool

Contains returns true if element is found in the Seq.

func ContainsFunc added in v0.6.0

func ContainsFunc[E any](s Seq[E], f func(e E) bool) bool

Contains returns true if element is satisfies the given predicate in the Seq.

func Count

func Count[E comparable](s Seq[E], value E) int

Count consumes the iterator, counting the number of elements equal to the given one and returning it.

Example:

iter.Count(seq(1,2,3,4,5,1,2,3), 2) // 2
iter.Count(seq(1,2,3,4,5,1,2,3), 5) // 1

func CountFunc added in v0.5.0

func CountFunc[E any](s Seq[E], f func(E) bool) int

CountFunc consumes the iterator, counting the number of elements match the predicate function and returning it.

Example:

iter.CountFunc(seq(1,2,3), func(x int) bool { return x % 2 == 0 }) // 1

func Find added in v0.5.0

func Find[E any](s Seq[E], f func(E) bool) (E, bool)

Find searches for an element of an iterator that satisfies a predicate.

func FindMap

func FindMap[E, T any](s Seq[E], f func(E) (T, bool)) (result T, ok bool)

FindMap applies function to the elements of iterator and returns the first non-none result.

func First added in v0.6.0

func First[E any](s Seq[E], f func(E) bool) (result E, ok bool)

First try to find first element in Seq, if no elements in Seq, return zero var and false.

Example:

iter.First(seq(1,2,3)) => 1, true
iter.First(seq[int]()) => 0, false

func Fold added in v0.5.0

func Fold[E, A any](s Seq[E], init A, f func(A, E) A) A

Fold each element into an accumulator by applying an operation, returning the final result.

Example:

iter.Fold(seq(1,2,3), 1, func(acc int, e int) int { return acc * e }) => 6

func ForEach

func ForEach[E any](s Seq[E], f func(E))

ForEach call f on each element in Seq.

func Index added in v0.5.0

func Index[E any](s Seq[E], f func(E) bool) int

Index searches index of element which satisfying the given predicate.

func IsSorted

func IsSorted[E cmp.Ordered](s Seq[E]) bool

IsSorted checks if the elements of this iterator are sorted.

Example:

iter.IsSorted(seq(1, 2, 3)) // true
iter.IsSorted(seq(2, 1, 3)) // false

func IsSortedFunc added in v0.5.0

func IsSortedFunc[E any](s Seq[E], f func(E, E) int) bool

IsSortedFunc checks if the elements of this iterator are sorted using the given comparator function.

Example:

iter.IsSortedFunc(seq(1, 2, 3), cmp.Compare[int]) // true
iter.IsSortedFunc(seq(2, 1, 3), cmp.Compare[int]) // false

func Last added in v0.6.0

func Last[E any](s Seq[E], f func(E) bool) (m E, ok bool)

Last try to find last element in Seq, if no elements in Seq, return zero var and false.

Example:

iter.Last(seq(1,2,3)) => 3, true
iter.Last(seq[int]()) => 0, false

func Max

func Max[E cmp.Ordered](s Seq[E]) (E, bool)

Max returns the maximum element of an iterator.

Example:

iter.Max(seq(1,2,3)) => 3, true
iter.Max(seq[int]()) => 0, false

func MaxByKey

func MaxByKey[E any, K cmp.Ordered](s Seq[E], f func(E) K) (E, bool)

MaxByKey returns the element that gives the maximum value from the specified function.

Example:

iter.MaxByKey(seq(user{name: "a"}, user{"name": "c"}), func(x user) string { return x.name }) => user{name: "b"}, true
iter.MaxByKey(seq(), func(x user) string { return x.name }) => nil, false

func MaxFunc added in v0.5.0

func MaxFunc[E any](s Seq[E], f func(E, E) int) (E, bool)

MaxFunc returns the element that gives the maximum value with respect to the specified comparison function.

Example:

iter.MaxFunc(seq(1,2,3), cmp.Compare[int]) => 3, true
iter.MaxFunc(seq[int](), cmp.Compare[int]) => 0, false

func Min

func Min[E cmp.Ordered](s Seq[E]) (E, bool)

Min returns the minimum element of an iterator.

Example:

iter.Min(seq(1,2,3)) // 1, true
iter.Min(seq[int]()) // 0, false

func MinByKey

func MinByKey[E any, K cmp.Ordered](s Seq[E], f func(E) K) (E, bool)

MinByKey returns the element that gives the minimum value from the specified function.

Example:

iter.MinByKey(seq(user{name: "a"}, user{"name": "c"}), func(x user) string { return x.name }) => user{name: "a"}, true
iter.MinByKey(seq(), func(x user) string { return x.name }) => nil, false

func MinFunc added in v0.5.0

func MinFunc[E any](s Seq[E], f func(E, E) int) (E, bool)

MinFunc returns the element that gives the minimum value with respect to the specified comparison function.

Example:

iter.MinFunc(seq(1,2,3), cmp.Compare[int]) // 1, true
iter.MinFunc(seq[int](), cmp.Compare[int]) // 0, false

func Reduce added in v0.5.0

func Reduce[E any](s Seq[E], f func(E, E) E) (result E, hasElem bool)

Reduce reduces the elements to a single one, by repeatedly applying a reducing operation.

Example:

iter.Reduce(seq(1,2,3), func(x, y int) int { return x * y }) => 6, true
iter.Reduce(seq[int](), func(x, y int) int { return x * y }) => 0, false

func Size added in v0.5.0

func Size[E any](s Seq[E]) int

Size consumes the iterator, counting the number of iterations and returning it.

Example:

iter.Size(seq(1,2,3)) // 3

func TryFold added in v0.5.0

func TryFold[E, A any](s Seq[E], init A, f func(A, E) (A, error)) (res A, err error)

TryFold applies a function as long as it returns successfully, producing a single, final value.

It takes two arguments: an initial value, and a closure with two arguments: an 'accumulator', and an element. The closure either returns successfully, with the value that the accumulator should have for the next iteration, or it returns failure, with an error value that is propagated back to the caller immediately (short-circuiting).

Example:

iter.TryFold(seq(1,2,3), 1, func(acc int, item int) (int, error) { return acc * item, nil }) => 6, nil
iter.TryFold(seq("1", "3", "e"), 1, func(acc int, item string) (int, error) {
	x, err := strconv.Atoi(item)
	if err != nil {return 0, err}
	return acc * x, nil
}) => 0, err

func TryForEach added in v0.5.0

func TryForEach[E any](s Seq[E], f func(E) error) (err error)

TryForEach call f on each element in Seq, stopping at the first error and returning that error.

Types

type Seq added in v0.5.0

type Seq[E any] func(yield func(E) bool)

Seq is an iterator over sequences of individual values. When called as seq(yield), seq calls yield(v) for each value v in the sequence, stopping early if yield returns false.

see: https://github.com/golang/go/issues/61897

func Append added in v0.6.0

func Append[E any](s Seq[E], elems ...E) Seq[E]

Append create a new Seq which yield each element from origin Seq and additional elements.

Example:

iter.Append(seq(1), 2, 3) => seq: 1,2,3
iter.Append(seq(1,2),) => seq: 1,2

func Chain

func Chain[E any](x Seq[E], y Seq[E]) Seq[E]

Chain takes two iterators and creates a new iterator over both in sequence.

Example:

iter.Chain(seq(1,2,3), seq(4,5,6)) => seq: 1,2,3,4,5,6

func Dedup added in v0.6.0

func Dedup[E comparable](s Seq[E]) Seq[E]

Dedup removes consecutive repeated elements in the Seq.

Example:

iter.Dedup(seq(1,1,2,2,3,3)) => seq: 1,2,3
iter.Dedup(seq(1,2,2,3,2,2)) => seq: 1,2,3,2

func DedupFunc added in v0.6.0

func DedupFunc[E any](s Seq[E], f func(E, E) bool) Seq[E]

DedupFunc removes all but the first of consecutive elements in the Seq satisfying a given equality relation.

Example:

iter.DedupFunc(seq(1,1,2,2,3,3), func(x int, y int) bool { return x + y < 5 }) => seq: 1,3
iter.DedupFunc(seq(1,2,3,2,1), func(x int, y int) bool { return x != y}) => seq: 1

func Enumerate

func Enumerate[E any](s Seq[E]) Seq[tuple.Pair[int, E]]

Enumerate create a new seq which yield (index, item) pair.

Example:

iter.Enumerate(seq(1,2,3)) => seq: (0, 1), (1, 2), (2, 3)

func Filter

func Filter[E any](s Seq[E], f func(E) bool) Seq[E]

Filter creates an iterator which uses a closure to determine if an element should be yielded.

Filter will not stop until iterate finished.

Example:

iter.Filter(seq(1,2,3), func(x int) bool {return i%2==1}) => seq: 1, 3

func FilterMap

func FilterMap[E, T any](s Seq[E], f func(E) (T, bool)) Seq[T]

FilterMap creates an iterator that both filters and maps.

Example:

slices.FilterMap(seq(1,2,3,4,5), func(x int) (string, bool) {
    if x%2 == 1 {
	    return strconv.Itoa(x), true
    }
    return "", false
}) => seq: "1", "3", "5"

func FilterZero added in v0.6.0

func FilterZero[E comparable](s Seq[E]) Seq[E]

FilterZero creates an iterator which remove zero variable from Seq.

Example:

iter.FilterZero(seq(true, false, false, true)) => seq: true, true
iter.FilterZero(seq(1, 0.0, 2., .3)) 		   => seq: 1, 2.0, 0.3

func FlatMap added in v0.5.0

func FlatMap[E, T any](s Seq[E], f func(E) Seq[T]) Seq[T]

FlatMap call f on each element in Seq which create a new type Seq by each element.

Example:

iter.FlatMap(seq(1,2,3), func(x int) Seq[int] { return rangeTo(x)}) // seq: 1,1,2,1,2,3

func Flatten added in v0.5.0

func Flatten[E any](s Seq[Seq[E]]) Seq[E]

func Inspect added in v0.6.0

func Inspect[E any](s Seq[E], f func(E)) Seq[E]

Inspect call f on each element in Seq.

Like ForEach, Inspect will iterate all element in Seq without stopping. Unlike ForEach, Inspect will not consume Seq.

Example:

iter.Inspect(seq(1,2,3), func(x int) { println(x) }) // seq: 1,2,3

func Intersperse added in v0.6.0

func Intersperse[E any](s Seq[E], sep E) Seq[E]

Intersperse creates a new iterator which places a copy of `separator` between adjacent items of the original iterator.

Example:

iter.Intersperse(seq(1,2,3), 0) // seq: 1,0,2,0,3

func Map

func Map[E, T any](s Seq[E], f func(E) T) Seq[T]

Map call f on each element in Seq, and map each element to another type.

Example:

iter.Map(seq(1,2,3), strconv.Itoa) => seq: "1", "2", "3"

func MapWhile

func MapWhile[E, T any](s Seq[E], f func(E) (T, bool)) Seq[T]

MapWhile call f on each element on Seq, and map each element to another type.

Stopping after an initial false.

Example:

iter.MapWhile(seq("1","2","e","3"), func(x string) (int, bool) {
	i, err := strconv.Atoi(x)
	if err != nil {
		return 0, false
	}
	return i, true
}) => seq: 1, 2

func Scan added in v0.6.0

func Scan[E, A any](s Seq[E], init A, f func(state A, elem E) A) Seq[A]

Scan returns a Seq containing successive accumulation values generated by applying [f] on each element and current accumulator value that starts with [init] value.

Example:

iter.Scan(seq(1,2,3,4,5),1,func(state int, elem int) int { return state * elem }) => seq: 1,2,6,24,120

func Skip

func Skip[E any](s Seq[E], n int) Seq[E]

Skip creates an iterator that skips the first `n` elements.

Example:

iter.Skip(seq(1,2,3), 1) // seq: 2,3
iter.Skip(seq(1,2), 3)   // seq: none

func SkipWhile added in v0.6.0

func SkipWhile[E any](s Seq[E], f func(E) bool) Seq[E]

SkipWhile creates an iterator that [`skip`]s elements based on a predicate.

Stop skip after an initial `false`.

func StepBy

func StepBy[E any](s Seq[E], n int) Seq[E]

StepBy creates an iterator starting at the same point, but stepping by the given amount at each iteration.

Example:

iter.StepBy(seq(1,2,3,4,5), 2) // seq: 1,3,5

func Take

func Take[E any](s Seq[E], n int) Seq[E]

Take creates an iterator that yields the first `n` elements, or fewer if the underlying iterator ends sooner.

Example:

iter.Take(seq(1,2,3), 2) // seq: 1,2
iter.Take(seq(1,2,3), 5) // seq: 1,2,3

func TakeWhile added in v0.6.0

func TakeWhile[E any](s Seq[E], f func(E) bool) Seq[E]

TakeWhile creates an iterator that yields elements based on a predicate.

Stopping after an initial `false`.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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