Documentation
¶
Overview ¶
Package iterp provides basic utilities for processing generic sequences. The supplemental packages iterp/slicep and iterp/mapp provide analogous utilities for slices and maps with consistent APIs.
Seq and Seq2 ¶
The iter.Seq and iter.Seq2 types are the primary abstractions in this package. Sequences may be infinite and they may not be replayable. This package should generally provide two implementations of each operations: one that operates on Seq and one that operates on Seq2. The "normal" version of a function operates on Seq values and the "2" variant of a function operates on Seq2 values. For example, Map takes a Seq[T] as input and produces a Seq[U] as output while Map2 takes a Seq2[T,U] as input and produces a Seq2[T,V] as output.
Map2 vs MapSeq2 ¶
Map2 returns an iter.Seq2 with the original sequence's "left" values paired with the mapped "right" values. This definition of Map2 makes the function work consistently with the Map2 functions for slices and maps which preserve the indices/keys of the original container.
MapSeq2 in this package provides a more general mapping for iter.Seq2 than is provided by Map2. But, the ability to rewrite the "left" value is not well defined for maps and slices. So while MapSeq2 can be used to create new maps and slices from existing ones, implementing this functionality is left up to individual applications which can apply domain specific knowledge to reason about collisions and/or "gaps" in the output keys/indices and determine appropriate semantics as necessary.
FoldLeft vs FoldRight ¶
The FoldLeft and FoldRight functions aggregate a sequence into an accumulator value. FoldLeft is defined as f(f(f(init, v1), v2), v3)... where v1, v2, v3... are the sequence elements in order. On the other hand,FoldRight is defined as f(v1, f(v2, f(v3,... f(vn, init)))), processing elements in reverse order. Because there is no memory-efficient way to right-fold a generic sequence the FoldRight function is only provided for slices.
Index ¶
- func AffineStep[Z Numeric](start Z, a, b Z) iter.Seq[Z]
- func Concat[T any](its ...iter.Seq[T]) iter.Seq[T]
- func Concat2[T any, U any](its ...iter.Seq2[T, U]) iter.Seq2[T, U]
- func Cons[T any](head T, tail iter.Seq[T]) iter.Seq[T]
- func DropN[T any](it iter.Seq[T], n int) iter.Seq[T]
- func DropWhile[T any](it iter.Seq[T], p PredicateFunc[T]) iter.Seq[T]
- func Empty[T any]() iter.Seq[T]
- func Empty2[T any, U any]() iter.Seq2[T, U]
- func Find[T any](it iter.Seq[T], p PredicateFunc[T]) (T, bool)
- func Find2[K any, T any](it iter.Seq2[K, T], p Predicate2Func[K, T]) (K, T, bool)
- func FlatMap[T any, U any](it iter.Seq[T], f MapFunc[T, iter.Seq[U]]) iter.Seq[U]
- func Flatten[T any](its iter.Seq[iter.Seq[T]]) iter.Seq[T]
- func Flatten2[T any, U any](its iter.Seq[iter.Seq2[T, U]]) iter.Seq2[T, U]
- func FoldLeft[T any, U any](it iter.Seq[T], init U, f FoldLFunc[T, U]) U
- func Ints[Z constraints.Integer](start Z) iter.Seq[Z]
- func IntsStep[Z constraints.Integer](start Z, step Z) iter.Seq[Z]
- func Left[T any, U any](it iter.Seq2[T, U]) iter.Seq[T]
- func List[T any](values ...T) iter.Seq[T]
- func List2[T any](values ...T) iter.Seq2[int, T]
- func Map[T any, U any](it iter.Seq[T], f MapFunc[T, U]) iter.Seq[U]
- func Map2[T any, U any, V any](it iter.Seq2[T, U], f Map2Func[T, U, V]) iter.Seq2[T, V]
- func MapSeq2[T any, U any, V any, W any](it iter.Seq2[T, U], f func(T, U) (V, W)) iter.Seq2[V, W]
- func Reject[T any](it iter.Seq[T], p PredicateFunc[T]) iter.Seq[T]
- func Repeat[T any](it iter.Seq[T], n int) iter.Seq[T]
- func RepeatedFunc[T any](start T, f func(T) T) iter.Seq[T]
- func Right[T any, U any](it iter.Seq2[T, U]) iter.Seq[U]
- func Select[T any](it iter.Seq[T], p PredicateFunc[T]) iter.Seq[T]
- func Sum[S Summable](it iter.Seq[S]) S
- func TakeN[T any](it iter.Seq[T], n int) iter.Seq[T]
- func TakeWhile[T any](it iter.Seq[T], p PredicateFunc[T]) iter.Seq[T]
- func Uniq[T comparable](it iter.Seq[T]) iter.Seq[T]
- func Unique[T comparable](it iter.Seq[T]) iter.Seq[T]
- func Zip[T any](its ...iter.Seq[T]) iter.Seq[[]T]
- func Zip2[T any, U any](v iter.Seq[T], w iter.Seq[U]) iter.Seq2[T, U]
- type FoldLFunc
- type FoldRFunc
- type Map2Func
- type MapFunc
- type Numeric
- type Predicate2Func
- type PredicateFunc
- type Summable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AffineStep ¶ added in v0.0.2
AffineStep returns a sequence of repeated applications of the affine function f(z) = a*z + b to the initial value start.
func Cons ¶ added in v0.0.2
Cons returns a sequence with head as the first element followed by the elements of tail.
func DropN ¶
DropN returns the suffix of it without the first n elements. If n is negative, the resulting sequence is it.
func DropWhile ¶
DropWhile returns the longest suffix of it for which p is false for the first element.
func Find ¶
func Find[T any](it iter.Seq[T], p PredicateFunc[T]) (T, bool)
Find returns the first element in it for which p is true along with the bool true. If there is no such element, it returns a zero value and false.
func Find2 ¶ added in v0.0.2
Find2 returns the first pair in it for which p is true along with the bool true. If there is no such pair, it returns zero values and false.
func Flatten2 ¶
Flatten2 concatenates a sequence of sequences of pairs to produce a single sequence of pairs.
func FoldLeft ¶
FoldLeft aggregates it into an accumulator initialized to init.
Note that a right fold over a generic sequence is very inefficient and it is not provided here. Slices can be right-folded.
func Ints ¶
func Ints[Z constraints.Integer](start Z) iter.Seq[Z]
Ints returns an infinite sequence of integers from start.
func IntsStep ¶
func IntsStep[Z constraints.Integer](start Z, step Z) iter.Seq[Z]
IntsStep returns an infinite sequence of integers from start and increasing by step.
func List2 ¶
List2 returns an ordered sequence of pairs containing the given values paired with their indices.
func Map2 ¶
Map2 is like Map but operates on a sequence of pairs. The resulting sequence has the same "left" values as the input and "right" values obtained by applying f to the input pairs.
func MapSeq2 ¶
MapSeq2 is like Map2 but the mapping function produces of the left and right values of the output sequence.
func Repeat ¶
Repeat returns a sequence that repeats the sequence it n times. If n is negative, the resulting sequence is infinite. Only replayable sequences can be repeated.
func RepeatedFunc ¶ added in v0.0.2
RepeatedFunc returns a sequence of repeated applications of the function: start, f(start), f(f(start)), ...
func TakeN ¶
TakeN returns the longest prefix of it with at most n elements. If n is negative, the resulting sequence is empty.
func Uniq ¶ added in v0.0.2
func Uniq[T comparable](it iter.Seq[T]) iter.Seq[T]
Uniq returns a sequence of values from it with consecutive duplicates removed much like the uniq command line utility. The iteration uses constant memory.
See Unique for building a sequence of truly unique values.
func Unique ¶ added in v0.0.2
func Unique[T comparable](it iter.Seq[T]) iter.Seq[T]
Unique returns a sequence that passes through unique elements of it the first time each is encountered. Unique uses memory proportional to the number of unique sequence elements.
See Uniq for a more memory efficient way of removing duplicates from (sorted) sequences.
Types ¶
type Numeric ¶ added in v0.0.2
type Numeric interface {
constraints.Integer | constraints.Float | constraints.Complex
}
Numeric is a constraint that matches all numeric types.
type Predicate2Func ¶ added in v0.0.2
PredicateFunc returns true for values that should be selected.
type PredicateFunc ¶ added in v0.0.2
PredicateFunc returns true for values that should be selected.
type Summable ¶
type Summable interface {
constraints.Integer | constraints.Float | constraints.Complex | ~string
}
Summable is a constraint that matches types that can be added using the + operator.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package mapp standard library's maps package by providing functions corresponding to sequence processing utilities in the iterp package.
|
Package mapp standard library's maps package by providing functions corresponding to sequence processing utilities in the iterp package. |
|
Package slicep supplements the standard library's slices package by providing functions corresponding to sequence processing utilities in the iterp package.
|
Package slicep supplements the standard library's slices package by providing functions corresponding to sequence processing utilities in the iterp package. |