Documentation
¶
Index ¶
- func All[E any](s Seq[E], f func(E) bool) bool
- func Any[E any](s Seq[E], f func(E) bool) (ok bool)
- func CollectFunc[E any](s Seq[E], collect func(E) bool)
- func Contains[E comparable](s Seq[E], x E) bool
- func ContainsFunc[E any](s Seq[E], f func(e E) bool) bool
- func Count[E comparable](s Seq[E], value E) int
- func CountFunc[E any](s Seq[E], f func(E) bool) int
- func Find[E any](s Seq[E], f func(E) bool) (E, bool)
- func FindMap[E, T any](s Seq[E], f func(E) (T, bool)) (result T, ok bool)
- func First[E any](s Seq[E], f func(E) bool) (result E, ok bool)
- func Fold[E, A any](s Seq[E], init A, f func(A, E) A) A
- func ForEach[E any](s Seq[E], f func(E))
- func Index[E any](s Seq[E], f func(E) bool) int
- func IsSorted[E cmp.Ordered](s Seq[E]) bool
- func IsSortedFunc[E any](s Seq[E], f func(E, E) int) bool
- func Last[E any](s Seq[E], f func(E) bool) (m E, ok bool)
- func Max[E cmp.Ordered](s Seq[E]) (E, bool)
- func MaxByKey[E any, K cmp.Ordered](s Seq[E], f func(E) K) (E, bool)
- func MaxFunc[E any](s Seq[E], f func(E, E) int) (E, bool)
- func Min[E cmp.Ordered](s Seq[E]) (E, bool)
- func MinByKey[E any, K cmp.Ordered](s Seq[E], f func(E) K) (E, bool)
- func MinFunc[E any](s Seq[E], f func(E, E) int) (E, bool)
- func Reduce[E any](s Seq[E], f func(E, E) E) (result E, hasElem bool)
- func Size[E any](s Seq[E]) int
- func TryFold[E, A any](s Seq[E], init A, f func(A, E) (A, error)) (res A, err error)
- func TryForEach[E any](s Seq[E], f func(E) error) (err error)
- type Seq
- func Append[E any](s Seq[E], elems ...E) Seq[E]
- func Chain[E any](x Seq[E], y Seq[E]) Seq[E]
- func Dedup[E comparable](s Seq[E]) Seq[E]
- func DedupFunc[E any](s Seq[E], f func(E, E) bool) Seq[E]
- func Enumerate[E any](s Seq[E]) Seq[tuple.Pair[int, E]]
- func Filter[E any](s Seq[E], f func(E) bool) Seq[E]
- func FilterMap[E, T any](s Seq[E], f func(E) (T, bool)) Seq[T]
- func FilterZero[E comparable](s Seq[E]) Seq[E]
- func FlatMap[E, T any](s Seq[E], f func(E) Seq[T]) Seq[T]
- func Flatten[E any](s Seq[Seq[E]]) Seq[E]
- func Inspect[E any](s Seq[E], f func(E)) Seq[E]
- func Intersperse[E any](s Seq[E], sep E) Seq[E]
- func Map[E, T any](s Seq[E], f func(E) T) Seq[T]
- func MapWhile[E, T any](s Seq[E], f func(E) (T, bool)) Seq[T]
- func Scan[E, A any](s Seq[E], init A, f func(state A, elem E) A) Seq[A]
- func Skip[E any](s Seq[E], n int) Seq[E]
- func SkipWhile[E any](s Seq[E], f func(E) bool) Seq[E]
- func StepBy[E any](s Seq[E], n int) Seq[E]
- func Take[E any](s Seq[E], n int) Seq[E]
- func TakeWhile[E any](s Seq[E], f func(E) bool) Seq[E]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶ added in v0.5.0
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
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
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
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
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 FindMap ¶
FindMap applies function to the elements of iterator and returns the first non-none result.
func First ¶ added in v0.6.0
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
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 IsSorted ¶
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
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
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 ¶
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 ¶
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
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 ¶
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 ¶
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
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
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
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
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
Types ¶
type Seq ¶ added in v0.5.0
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
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 ¶
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
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 ¶
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 ¶
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 ¶
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
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 Inspect ¶ added in v0.6.0
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
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 ¶
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 ¶
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
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 ¶
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
SkipWhile creates an iterator that [`skip`]s elements based on a predicate.
Stop skip after an initial `false`.
func StepBy ¶
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