Documentation
¶
Overview ¶
Package iter provides definition of generic iterator Iter and high-order functions.
Please refer to README.md for details.
Example ¶
Convert an int slice to string slice.
s := ToSlice(
Map(strconv.Itoa,
Filter(gvalue.IsNotZero[int],
FromSlice([]int{0, 1, 2, 3, 4}))))
fmt.Printf("%q\n", s)
Output: ["1" "2" "3" "4"]
Index ¶
- Constants
- func All[T any](f func(T) bool, i Iter[T]) bool
- func And[T ~bool](i Iter[T]) bool
- func Any[T any](f func(T) bool, i Iter[T]) bool
- func At[T any](idx int, i Iter[T]) (r goption.O[T])
- func Avg[T constraints.Number](i Iter[T]) float64
- func AvgBy[T any, N constraints.Number](f func(T) N, i Iter[T]) float64
- func Contains[T comparable](v T, i Iter[T]) bool
- func ContainsAll[T comparable](vs []T, i Iter[T]) bool
- func ContainsAny[T comparable](vs []T, i Iter[T]) bool
- func Count[T any](i Iter[T]) int
- func Find[T any](f func(T) bool, i Iter[T]) (r goption.O[T])
- func Fold[T1, T2 any](f func(T1, T2) T1, init T1, i Iter[T2]) T1
- func ForEach[T any](f func(v T), i Iter[T])
- func ForEachIndexed[T any](f func(i int, v T), i Iter[T])
- func GroupBy[K comparable, T any](f func(T) K, i Iter[T]) map[K][]T
- func Head[T any](i Iter[T]) (r goption.O[T])
- func Join[T ~string](sep T, i Iter[T]) T
- func KVToMap[K comparable, V any](i Iter[tuple.T2[K, V]]) map[K]V
- func Max[T constraints.Ordered](i Iter[T]) (r goption.O[T])
- func MaxBy[T any](less func(T, T) bool, i Iter[T]) (r goption.O[T])
- func Min[T constraints.Ordered](i Iter[T]) (r goption.O[T])
- func MinBy[T any](less func(T, T) bool, i Iter[T]) (r goption.O[T])
- func MinMax[T constraints.Ordered](i Iter[T]) (r goption.O[tuple.T2[T, T]])
- func MinMaxBy[T any](less func(T, T) bool, i Iter[T]) (r goption.O[tuple.T2[T, T]])
- func Or[T ~bool](i Iter[T]) bool
- func Reduce[T any](f func(T, T) T, i Iter[T]) (r goption.O[T])
- func Sum[T constraints.Number](i Iter[T]) T
- func SumBy[T any, N constraints.Number](f func(T) N, i Iter[T]) N
- func ToBufferedChan[T any](ctx context.Context, size int, i Iter[T]) <-chan T
- func ToChan[T any](ctx context.Context, i Iter[T]) <-chan T
- func ToMap[K comparable, V, T any](f func(T) (K, V), i Iter[T]) map[K]V
- func ToMapValues[K comparable, T any](f func(T) K, i Iter[T]) map[K]T
- func ToSlice[T any](i Iter[T]) []T
- type Iter
- func Append[T any](tail T, i Iter[T]) Iter[T]
- func Chunk[T any](n int, i Iter[T]) Iter[[]T]
- func Compact[T comparable](i Iter[T]) Iter[T]
- func Concat[T any](is ...Iter[T]) Iter[T]
- func Divide[T any](n int, i Iter[T]) Iter[[]T]
- func Drop[T any](n int, i Iter[T]) Iter[T]
- func DropWhile[T any](f func(T) bool, i Iter[T]) Iter[T]
- func Dup[T comparable](i Iter[T]) Iter[T]
- func DupBy[T any, K comparable](f func(T) K, i Iter[T]) Iter[T]
- func Filter[T any](f func(T) bool, i Iter[T]) Iter[T]
- func FilterMap[T1, T2 any](f func(T1) (T2, bool), i Iter[T1]) Iter[T2]
- func FlatMap[T1, T2 any](f func(T1) []T2, i Iter[T1]) Iter[T2]
- func FromChan[T any](ctx context.Context, ch <-chan T) Iter[T]
- func FromMap[K comparable, V any](m map[K]V) Iter[tuple.T2[K, V]]
- func FromMapKeys[K comparable, V any](m map[K]V) Iter[K]
- func FromMapValues[K comparable, V any](m map[K]V) Iter[V]
- func FromSlice[T any](s []T) Iter[T]
- func Intersperse[T any](sep T, i Iter[T]) Iter[T]
- func Map[T1, T2 any](f func(T1) T2, i Iter[T1]) Iter[T2]
- func MapInplace[T any](f func(T) T, i Iter[T]) Iter[T]
- func Prepend[T any](head T, i Iter[T]) Iter[T]
- func Range[T constraints.Number](start, stop T) Iter[T]
- func RangeWithStep[T constraints.Number](start, stop, step T) Iter[T]
- func Remove[T comparable](v T, i Iter[T]) Iter[T]
- func RemoveN[T comparable](v T, n int, i Iter[T]) Iter[T]
- func Repeat[T any](v T) Iter[T]
- func Reverse[T any](i Iter[T]) Iter[T]
- func Shuffle[T any](i Iter[T]) Iter[T]
- func Sort[T constraints.Ordered](i Iter[T]) Iter[T]
- func SortBy[T any](less func(T, T) bool, i Iter[T]) Iter[T]
- func StableSortBy[T any](less func(T, T) bool, i Iter[T]) Iter[T]
- func StealSlice[T any](s []T) Iter[T]
- func Take[T any](n int, i Iter[T]) Iter[T]
- func TakeWhile[T any](f func(T) bool, i Peeker[T]) Iter[T]
- func TypeAssert[To, From any](i Iter[From]) Iter[To]
- func Uniq[T comparable](i Iter[T]) Iter[T]
- func UniqBy[T any, K comparable](f func(T) K, i Iter[T]) Iter[T]
- func Zip[T1, T2, T3 any](f func(T1, T2) T3, a Peeker[T1], b Peeker[T2]) Iter[T3]
- type Peeker
Examples ¶
Constants ¶
const (
ALL = -1
)
Variables ¶
This section is empty.
Functions ¶
func Any ¶
Any determines whether any (at least one) element of the iterator i satisfies the predicate f.
Any supports short-circuit evaluation.
func At ¶
At returns the possible element at given 0-based index idx.
⚠️ WARNING: Panic when index < 0.
func Avg ¶
func Avg[T constraints.Number](i Iter[T]) float64
Avg returns the arithmetic mean of elements of iterator i.
func AvgBy ¶
func AvgBy[T any, N constraints.Number](f func(T) N, i Iter[T]) float64
AvgBy applies function f to each element of iterator i, returns the arithmetic mean of function result.
func Contains ¶
func Contains[T comparable](v T, i Iter[T]) bool
Contains returns whether the element occur in iterator.
func ContainsAll ¶
func ContainsAll[T comparable](vs []T, i Iter[T]) bool
ContainsAll returns whether the all of given elements occur in iterator.
func ContainsAny ¶
func ContainsAny[T comparable](vs []T, i Iter[T]) bool
ContainsAny returns whether the any one of given elements occur in iterator.
func Fold ¶
Fold applies function f cumulatively to each element of iterator i, so as to fold the Iter to a single value.
An init element is needed as the initial value of accumulation.
func ForEachIndexed ¶
ForEachIndexed applies function f to each element of iterator i. The argument i of function f represents the zero-based index of that element of iterator.
func GroupBy ¶
func GroupBy[K comparable, T any](f func(T) K, i Iter[T]) map[K][]T
GroupBy groups adjacent elements according to key returned by function f.
func KVToMap ¶
func KVToMap[K comparable, V any](i Iter[tuple.T2[K, V]]) map[K]V
KVToMap collects elements of iterator to a map.
If the iterator is empty, empty map map[K]V{} is returned.
func Max ¶
func Max[T constraints.Ordered](i Iter[T]) (r goption.O[T])
Max returns the maximum element of iterator i.
func Min ¶
func Min[T constraints.Ordered](i Iter[T]) (r goption.O[T])
Min returns the minimum element of iterator i.
func MinMax ¶
MinMax returns both minimum and maximum elements of iterator i.
💡 NOTE: The returned min and max elements may be the same object when each element of the iterator is equal
💡 AKA: Bound
func MinMaxBy ¶
MinMaxBy returns both minimum and maximum elements of iterator i determined by function less.
💡 AKA: BoundBy
func Or ¶
Or determines whether any (at least one) element of the iterator i is true.
Or supports short-circuit evaluation.
func Reduce ¶
Reduce is a variant of Fold, use the first element of iterator as the initial value of accumulation.
func Sum ¶
func Sum[T constraints.Number](i Iter[T]) T
Sum returns the arithmetic sum of the elements of iterator i.
💡 NOTE: The returned type is still T, it may overflow for smaller types (such as int8, uint8).
func SumBy ¶
func SumBy[T any, N constraints.Number](f func(T) N, i Iter[T]) N
SumBy applies function f to each element of iterator i, returns the arithmetic sum of function result.
func ToBufferedChan ¶
ToBufferedChan collects elements of iterator to a buffered chan with given size.
The returned channel will be closed when iterator is exhausted.
func ToChan ¶
ToChan collects elements of iterator to a chan. The returned channel will be closed when iterator is exhausted.
func ToMap ¶
func ToMap[K comparable, V, T any](f func(T) (K, V), i Iter[T]) map[K]V
ToMap collects elements of iterator i to a map, both map keys and values are produced by mapping function f.
If the iterator is empty, empty map map[K]V{} is returned.
func ToMapValues ¶
func ToMapValues[K comparable, T any](f func(T) K, i Iter[T]) map[K]T
ToMapValues collects elements of iterator to values of map, the map keys are produced by mapping function f.
If the iterator is empty, empty map map[K]T{} is returned.
Types ¶
type Iter ¶
type Iter[T any] interface { // Next returns N items of iterator when it is not empty. // When the iterator is empty, nil is returned. // When n = [ALL] or n is greater than the number of remaining elements, // all remaining are returned. // // The returned slice is owned by caller. So implementer should return a // newly allocated slice if needed. // // Passing in a negative n (except [ALL]) is undefined behavior. Next(n int) []T }
Iter is a generic iterator interface, which helps us iterate various data structures in the same way.
Users can apply various operations (Map, Filter, etc.) on custom data structures by implementing Iter for them. See ExampleIter_impl for details.
Example (Impl) ¶
Iter for container list.
l := list.New() l.PushBack(0) l.PushBack(1) l.PushBack(2) s := ToSlice( Filter(gvalue.IsNotZero[int], FromList[int](l))) fmt.Println(s)
Output: [1 2]
func Chunk ¶
Chunk splits a list into length-n chunks and returns chunks by a new iterator.
The last chunk will be shorter if n does not evenly divide the length of the iterator.
💡 HINT: If you want to splits list into n chunks, use function Divide.
func Compact ¶
func Compact[T comparable](i Iter[T]) Iter[T]
Compact removes all zero value from given iterator i, returns a new iterator.
💡 HINT: See github.com/bytedance/gg/gvalue.Zero for details of zero value.
func Divide ¶
Divide splits a list into exactly n chunks and returns chunks by a new iterator.
The length of chunks will be different if n does not evenly divide the length of the iterator.
💡 HINT: If you want to splits list into length-n chunks, use function Chunk.
func Drop ¶
Drop drops the first n elements of iterator, returns the remaining part of slice, or empty iterator if n > len(s).
⚠️ WARNING: Panic when n < 0.
func DropWhile ¶
DropWhile returns the elements when the predicate f fails for the first time till the end of the iterator.
In other words, it returns the suffix remaining after TakeWhile(f, i).
func Dup ¶
func Dup[T comparable](i Iter[T]) Iter[T]
Dup returns the repeated elements of iterator. The result is a new iterators contains duplicate elements.
func DupBy ¶
func DupBy[T any, K comparable](f func(T) K, i Iter[T]) Iter[T]
DupBy distinguishes repeated elements with key function f, returns the duplicate elements of iterator. The result is a new iterators contains duplicate elements.
func Filter ¶
Filter applies predicate f to each element of iterator i, returns those elements that satisfy the predicate f as a new Iter.
func FilterMap ¶
FilterMap applies function f to each element of iterator i with Type T1. If f return false, the element i will be removed Results of f are returned as a new Iter with Type T2.
func FlatMap ¶
FlatMap applies function f to each element of iterator i with Type T1. Results of f are flattened and returned as a new Iter with Type T2.
func FromChan ¶
FromChan constructs an Iter from channel ch. Elements in Iter are exhausted when the context is done or given channel is closed. FIXME: better doc
func FromMap ¶
func FromMap[K comparable, V any](m map[K]V) Iter[tuple.T2[K, V]]
FromMap constructs an Iter of (key, value) pair from map m.
💡 NOTE: Function follows the same iteration semantics as a range statement. See https://go.dev/blog/maps#iteration-order for details.
func FromMapKeys ¶
func FromMapKeys[K comparable, V any](m map[K]V) Iter[K]
FromMapKeys constructs an Iter of map's key from map m.
💡 NOTE: Function follows the same iteration semantics as a range statement. See https://go.dev/blog/maps#iteration-order for details.
func FromMapValues ¶
func FromMapValues[K comparable, V any](m map[K]V) Iter[V]
FromMapValues constructs an Iter of map's value from map m.
💡 NOTE: Function follows the same iteration semantics as a range statement. See https://go.dev/blog/maps#iteration-order for details.
func FromSlice ¶
FromSlice constructs an Iter from slice s, in order from left to right. An empty Iter (without element) is returned if the given slice is empty or nil.
func Intersperse ¶
Intersperse intersperses value sep between the elements of iterator i.
func Map ¶
Map applies function f to each element of iterator i with Type T1. Results of f are returned as a new Iter with Type T2.
func MapInplace ¶
MapInplace is a closed Map operation, applies function f to each element. Results of f are returned as a new iterator with same type.
💡 HINT: MapInplace can reuse the underlying buffer of iterator, so it is more efficient than Map.
func Range ¶
func Range[T constraints.Number](start, stop T) Iter[T]
Range is a variant of RangeWithStep, with predefined step 1.
func RangeWithStep ¶
func RangeWithStep[T constraints.Number](start, stop, step T) Iter[T]
RangeWithStep constructs an Iter of number from start (inclusive) to stop (exclusive) by step. If the interval does not exist, RangeWithStep returns an emptyIter.
func Remove ¶
func Remove[T comparable](v T, i Iter[T]) Iter[T]
Remove removes all element v from the iterator i, returns a new iterator.
func RemoveN ¶
func RemoveN[T comparable](v T, n int, i Iter[T]) Iter[T]
Remove removes the first N element v from the iterator i, returns a new iterator.
func Shuffle ¶
Shuffle pseudo-randomizes the order of elements of iterator i and returns a new iterator.
func Sort ¶
func Sort[T constraints.Ordered](i Iter[T]) Iter[T]
Sort sorts elements of iterators i, returns a new iterator.
TODO: Sort is not lazy for now.
func SortBy ¶
SortBy sorts elements of iterators i with function less, returns a new iterator.
TODO: SortBy is not lazy for now.
func StableSortBy ¶
StableSortBy is variant of SortBy, it keeps the original order of equal elements when sorting.
TODO: StableSortBy is not lazy for now.
func StealSlice ¶
func Take ¶
Take returns the first n elements of iterator, or iterator itself if n > len(s).
⚠️ WARNING: Panic when n < 0.
func TakeWhile ¶
TakeWhile returns the longest prefix (possibly empty) of iterator i of elements that satisfy predicate f.
func TypeAssert ¶
TypeAssert converts a iterator from type From to type To by type assertion.
⚠️ WARNING: Type assertion is not type conversion/casting. See github.com/bytedance/gg/gvalue.TypeAssert for more details.
🚀 EXAMPLE:
ToSlice(TypeAssert[int](FromSlice([]any{1, 2, 3}))) ⏩ []int{1, 2, 3}
func Uniq ¶
func Uniq[T comparable](i Iter[T]) Iter[T]
Uniq returns the distinct elements of iterator. The result is a new iterators contains no duplicate elements.
func UniqBy ¶
func UniqBy[T any, K comparable](f func(T) K, i Iter[T]) Iter[T]
UniqBy distinguishes different elements with key function f, returns the distinct elements of iterator. The result is a new iterators contains no duplicate elements.
type Peeker ¶
type Peeker[T any] interface { Iter[T] // Peek returns the next N element of iterator without consuming it. Peek(n int) []T }
Peeker is also an iterator interface, which allows users to peek on the elements without consuming them.
Peeker is required by some operations like Equal and Zip. Use ToPeeker to wrap an Iter as a Peeker.