xiter

package module
v0.0.0-...-c999f37 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 7 Imported by: 4

README

xiter

Go Reference

xiter is a Go package offering a variety of tools for sequence manipulation and functional-style operations on sequences. It provides a range of functions for concatenation, transformation, filtering, searching, and more, for different types of sequences.

Installation

To install xiter, use the following command:

go get spheric.cloud/xiter

Usage

Here's an example demonstrating some functionalities of xiter:

package main

import (
    "fmt"
    "spheric.cloud/xiter"
)

func main() {
    // Example: Concatenating two sequences
    seq1 := xiter.Of(1, 2, 3)
    seq2 := xiter.Of(4, 5, 6)
    concatenated := xiter.Concat(seq1, seq2)
    fmt.Println("Concatenated Sequence:", xiter.ToSlice(concatenated))

    // Example: Filtering a sequence
    filtered := xiter.Filter(func(v int) bool { return v%2 == 0 }, concatenated)
    fmt.Println("Filtered Sequence (Even Numbers):", xiter.ToSlice(filtered))

    // Example: Mapping a sequence
    mapped := xiter.Map(func(v int) int { return v * v }, filtered)
    fmt.Println("Mapped Sequence (Squared Values):", xiter.ToSlice(mapped))
}

API Reference

The xiter package includes a variety of types and functions, such as:

  • Sequence types: Seq0, Seq[V], Seq2[K, V]
  • Concatenation functions: Concat0, Concat[V], Concat2[K, V]
  • Transformation functions: Pull0, Pull[V], Pull2[K, V]
  • Zipping functions: Zip0, Zip[V1, V2], Zip2[K1, V1, K2, V2]
  • Utility functions: Equal0, Equal[V], Equal2[K, V], Foreach[V], Map[In, Out]

For full documentation, visit pkg.go.dev.

Contributing

Contributions to xiter are welcome! Please refer to the contributing guidelines for more information.

License

xiter is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[V any](seq iter.Seq[V], f func(V) bool) bool

func All2

func All2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) bool

func Any

func Any[V any](seq iter.Seq[V], f func(V) bool) bool

func Any2

func Any2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) bool

func AppendKVSlice2

func AppendKVSlice2[S ~[]any, K, V any](s S, seq iter.Seq2[K, V]) S

func AppendSlice

func AppendSlice[S ~[]V, V any](s S, seq iter.Seq[V]) S

func AppendSlice2

func AppendSlice2[S ~[]V, V any](s S, seq iter.Seq2[V, V]) S

func AppendSliceMap

func AppendSliceMap[M ~map[K][]V, K comparable, V any](m M, seq iter.Seq2[K, V])

func Cache

func Cache[V any](seq iter.Seq[V]) (res iter.Seq[V], stop func())

func Cache2

func Cache2[K, V any](seq iter.Seq2[K, V]) (res iter.Seq2[K, V], stop func())

func Concat

func Concat[V any](seqs ...iter.Seq[V]) iter.Seq[V]

Concat concatenates multiple Seq into a single Seq.

func Concat2

func Concat2[K, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V]

Concat2 concatenates multiple Seq2 into a single Seq2.

func Contains

func Contains[V comparable](seq iter.Seq[V], needle V) bool

func Contains2

func Contains2[K, V comparable](seq iter.Seq2[K, V], needleK K, needleV V) bool

func ContainsFunc

func ContainsFunc[V any](seq iter.Seq[V], f func(V) bool) bool

func ContainsFunc2

func ContainsFunc2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) bool

func CopyToSlice

func CopyToSlice[S ~[]V, V any](dst S, seq iter.Seq[V]) int

func Count

func Count[V any](seq iter.Seq[V], f func(V) bool) int

func Count2

func Count2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) int

func Cycle

func Cycle[V any](seq iter.Seq[V]) (iter.Seq[V], func())

func Cycle2

func Cycle2[K, V any](seq iter.Seq2[K, V]) (res iter.Seq2[K, V], stop func())

func Deref

func Deref[V any](seq iter.Seq[*V]) iter.Seq[V]

Deref returns a sequence that dereferences the original sequence values.

func Drain

func Drain[V any](seq iter.Seq[V])

func Drain2

func Drain2[K, V any](seq iter.Seq2[K, V])

func Drop

func Drop[V any](seq iter.Seq[V], n int) iter.Seq[V]

func Drop2

func Drop2[K, V any](seq iter.Seq2[K, V], n int) iter.Seq2[K, V]

func DropWhile

func DropWhile[V any](seq iter.Seq[V], f func(V) bool) iter.Seq[V]

func DropWhile2

func DropWhile2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) iter.Seq2[K, V]

func Empty

func Empty[V any]() iter.Seq[V]

func Empty2

func Empty2[K, V any]() iter.Seq2[K, V]

func Enumerate

func Enumerate[V any](seq iter.Seq[V]) iter.Seq2[int, V]

func Equal

func Equal[V comparable](seq1, seq2 iter.Seq[V]) bool

Equal checks if two Seq sequences are equal by iterating and checking if both have the same length and values.

func Equal2

func Equal2[K, V comparable](seq1, seq2 iter.Seq2[K, V]) bool

Equal2 checks if two Seq2 sequences are equal by iterating and checking if both have the same length and values.

func EqualFunc

func EqualFunc[V1, V2 any](seq1 iter.Seq[V1], seq2 iter.Seq[V2], f func(V1, V2) bool) bool

func EqualFunc2

func EqualFunc2[K1, V1, K2, V2 any](seq1 iter.Seq2[K1, V1], seq2 iter.Seq2[K2, V2], f func(K1, V1, K2, V2) bool) bool

func Filter

func Filter[V any](seq iter.Seq[V], f func(V) bool) iter.Seq[V]

func Filter2

func Filter2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) iter.Seq2[K, V]

func FilterErr

func FilterErr[V any](seq iter.Seq[V], f func(V) (bool, error)) iter.Seq2[V, error]

FilterErr filters the sequence with the predicate function. If it errors, the error and the value it errored for is yielded.

func FilterKey

func FilterKey[K, V any](seq iter.Seq2[K, V], f func(K) bool) iter.Seq2[K, V]

func FilterKeyNotEqual

func FilterKeyNotEqual[K comparable, V any](seq iter.Seq2[K, V], kNotEq K) iter.Seq2[K, V]

func FilterNotEqual

func FilterNotEqual[V comparable](seq iter.Seq[V], notEq V) iter.Seq[V]

func FilterNotEqual2

func FilterNotEqual2[K, V comparable](seq iter.Seq2[K, V], kNotEq K, vNotEq V) iter.Seq2[K, V]

func FilterValue

func FilterValue[K, V any](seq iter.Seq2[K, V], f func(V) bool) iter.Seq2[K, V]

func FilterValueNotEqual

func FilterValueNotEqual[K any, V comparable](seq iter.Seq2[K, V], vNotEq V) iter.Seq2[K, V]

func Find

func Find[V any](seq iter.Seq[V], f func(V) bool) (V, bool)

func Find2

func Find2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) (K, V, bool)

func FindByKey

func FindByKey[K, V any](seq iter.Seq2[K, V], f func(K) bool) (K, V, bool)

func FindByValue

func FindByValue[K, V any](seq iter.Seq2[K, V], f func(V) bool) (K, V, bool)

func Flatmap

func Flatmap[In, Out any](seq iter.Seq[In], f func(In) iter.Seq[Out]) iter.Seq[Out]

func Flatmap2

func Flatmap2[KIn, VIn, KOut, VOut any](seq iter.Seq2[KIn, VIn], f func(KIn, VIn) iter.Seq2[KOut, VOut]) iter.Seq2[KOut, VOut]

func Flatten

func Flatten[V any](seq iter.Seq[iter.Seq[V]]) iter.Seq[V]

func Flatten2

func Flatten2[K, V any](seq iter.Seq[iter.Seq2[K, V]]) iter.Seq2[K, V]

func Foreach

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

func Foreach2

func Foreach2[K, V any](seq iter.Seq2[K, V], f func(K, V))

func Grouped

func Grouped[V any](seq iter.Seq[V], n int) iter.Seq[[]V]

func GroupedNoCopy

func GroupedNoCopy[V any](seq iter.Seq[V], n int) iter.Seq[[]V]

func Index

func Index[V any](seq iter.Seq[V], idx int) V

func Index2

func Index2[K, V any](seq iter.Seq2[K, V], idx int) (K, V)

func IntoChan

func IntoChan[C ~chan<- V, V any](c C, seq iter.Seq[V])

func Join

func Join[V ~string](seq iter.Seq[V], sep string) string

func Keys

func Keys[K, V any](seq iter.Seq2[K, V]) iter.Seq[K]

func Len

func Len[V any](s iter.Seq[V]) int

func Len2

func Len2[K, V any](s iter.Seq2[K, V]) int

func Map

func Map[In, Out any](seq iter.Seq[In], f func(In) Out) iter.Seq[Out]

func Map2

func Map2[KIn, VIn, KOut, VOut any](seq iter.Seq2[KIn, VIn], f func(KIn, VIn) (KOut, VOut)) iter.Seq2[KOut, VOut]

func MapErr

func MapErr[In, Out any](seq iter.Seq[In], f func(In) (Out, error)) iter.Seq2[Out, error]

func MapKeys

func MapKeys[KIn, V, KOut any](seq iter.Seq2[KIn, V], f func(KIn) KOut) iter.Seq2[KOut, V]

func MapLift

func MapLift[In, KOut, VOut any](seq iter.Seq[In], f func(In) (KOut, VOut)) iter.Seq2[KOut, VOut]

func MapLower

func MapLower[KIn, VIn, VOut any](seq iter.Seq2[KIn, VIn], f func(KIn, VIn) VOut) iter.Seq[VOut]

func MapValues

func MapValues[K, VIn, VOut any](seq iter.Seq2[K, VIn], f func(VIn) VOut) iter.Seq2[K, VOut]

func Max

func Max[V cmp.Ordered](seq iter.Seq[V]) V

func MaxFunc

func MaxFunc[V cmp.Ordered](seq iter.Seq[V], compare func(a, b V) int) V

func MaxOk

func MaxOk[V cmp.Ordered](seq iter.Seq[V]) (V, bool)

func MaxOkFunc

func MaxOkFunc[V cmp.Ordered](seq iter.Seq[V], compare func(a, b V) int) (V, bool)

func Merge

func Merge[K, V any](seq1 iter.Seq[K], seq2 iter.Seq[V]) iter.Seq2[K, V]

Merge returns a sequence that is the result of merging both sequence values. If one of both sequences do not yield a value anymore, the sequence returns.

func MergeAll

func MergeAll[K, V any](seq1 iter.Seq[K], seq2 iter.Seq[V], defV1 K, defV2 V) iter.Seq2[K, V]

func MergeAllFunc

func MergeAllFunc[K, V any](seq1 iter.Seq[K], seq2 iter.Seq[V], defV1Func func() K, defV2Func func() V) iter.Seq2[K, V]

func Min

func Min[V cmp.Ordered](seq iter.Seq[V]) V

func MinFunc

func MinFunc[V cmp.Ordered](seq iter.Seq[V], compare func(a, b V) int) V

func MinOk

func MinOk[V cmp.Ordered](seq iter.Seq[V]) (V, bool)

func MinOkFunc

func MinOkFunc[V cmp.Ordered](seq iter.Seq[V], compare func(a, b V) int) (V, bool)

func Of

func Of[V any](vs ...V) iter.Seq[V]

func OfChan

func OfChan[C interface{ ~<-chan V | ~chan V }, V any](c C) iter.Seq[V]

func OfKVSlice

func OfKVSlice[K, V any, S ~[]any](s S) iter.Seq2[K, V]

func OfKVs

func OfKVs[K, V any](kvs ...any) iter.Seq2[K, V]

func OfMap

func OfMap[M ~map[K]V, K comparable, V any](m M) iter.Seq2[K, V]

func OfMapKeys

func OfMapKeys[M ~map[K]V, K comparable, V any](m M) iter.Seq[K]

func OfMapValues

func OfMapValues[M ~map[K]V, K comparable, V any](m M) iter.Seq[V]

func OfNext

func OfNext[V any](f func() (V, bool)) iter.Seq[V]

func OfNext2

func OfNext2[K, V any](f func() (K, V, bool)) iter.Seq2[K, V]

func OfSlice

func OfSlice[S ~[]V, V any](s S) iter.Seq[V]

func OfSliceIndex

func OfSliceIndex[S ~[]V, V any](s S) iter.Seq2[int, V]

func OfSlicePtr

func OfSlicePtr[S ~[]V, V any](s S) iter.Seq[*V]

OfSlicePtr iterates over pointers of the slice elements.

func OfSlicePtrIndex

func OfSlicePtrIndex[S ~[]V, V any](s S) iter.Seq2[int, *V]

OfSlicePtrIndex iterates over pointers and indices of the slice elements.

func Range

func Range(start, end int) iter.Seq[int]

func RangeStep

func RangeStep(start, end, step int) iter.Seq[int]

func Receive

func Receive[C interface{ ~<-chan V | chan V }, V any](ctx context.Context, c C) iter.Seq[V]

func Reduce

func Reduce[Sum, V any](sum Sum, seq iter.Seq[V], f func(Sum, V) Sum) Sum

func Reduce2

func Reduce2[Sum, K, V any](sum Sum, seq iter.Seq2[K, V], f func(Sum, K, V) Sum) Sum

func Ref

func Ref[V any](seq iter.Seq[V]) iter.Seq[*V]

Ref returns a sequence that takes references of the original sequence values.

func Repeat

func Repeat[V any](v V, n int) iter.Seq[V]

func Repeat2

func Repeat2[K, V any](k K, v V, n int) iter.Seq2[K, V]

func Send

func Send[C interface{ ~chan<- V | chan V }, V any](ctx context.Context, c C, seq iter.Seq[V])

func SendChan

func SendChan[V any](ctx context.Context, seq iter.Seq[V]) <-chan V

func Separate

func Separate[K, V any](seq iter.Seq2[K, V]) (iter.Seq[K], iter.Seq[V], func())

func SetMap

func SetMap[M ~map[K]V, K comparable, V any](m M, seq iter.Seq2[K, V])

func Sum

func Sum[V cmp.Ordered](seq iter.Seq[V]) V

func Swap

func Swap[K, V any](seq iter.Seq2[K, V]) iter.Seq2[V, K]

func Take

func Take[V any](seq iter.Seq[V], n int) iter.Seq[V]

func Take2

func Take2[K, V any](seq iter.Seq2[K, V], n int) iter.Seq2[K, V]

func TakeWhile

func TakeWhile[V any](seq iter.Seq[V], f func(V) bool) iter.Seq[V]

func TakeWhile2

func TakeWhile2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) iter.Seq2[K, V]

func Tap

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

func Tap2

func Tap2[K, V any](seq iter.Seq2[K, V], f func(K, V)) iter.Seq2[K, V]

func TapKey

func TapKey[K, V any](seq iter.Seq2[K, V], f func(K)) iter.Seq2[K, V]

func TapValue

func TapValue[K, V any](seq iter.Seq2[K, V], f func(V)) iter.Seq2[K, V]

func ToChan

func ToChan[V any](seq iter.Seq[V]) <-chan V

func ToKVSlice

func ToKVSlice[K, V any](seq iter.Seq2[K, V]) []any

func ToList

func ToList[V any](seq iter.Seq[V]) *list.List

func ToMap

func ToMap[K comparable, V any](seq iter.Seq2[K, V]) map[K]V

func ToSlice

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

func ToSlice2

func ToSlice2[V any](seq iter.Seq2[V, V]) []V

func ToSliceMap

func ToSliceMap[K comparable, V any](seq iter.Seq2[K, V]) map[K][]V

func ToSliceWithCap

func ToSliceWithCap[V any](seq iter.Seq[V], cap int) []V

func ToSliceWithCap2

func ToSliceWithCap2[V any](seq iter.Seq2[V, V], cap int) []V

func TryAppend

func TryAppend[S ~[]K, K any](s S, it iter.Seq2[K, error]) ([]K, error)

TryAppend appends non-error values to the given slice. If an error is encountered, the slice and encountered error are returned immediately.

func TryAppendDeref

func TryAppendDeref[S ~[]K, K any](s S, it iter.Seq2[*K, error]) ([]K, error)

TryAppendDeref appends dereferenced non-error values to the given slice. If an error is encountered, the slice and encountered error are returned immediately.

func TryCollect

func TryCollect[K any](it iter.Seq2[K, error]) ([]K, error)

TryCollect collects non-error values in a slice. If an error is encountered, the slice and encountered error are returned immediately.

func TryCollectDeref

func TryCollectDeref[K any](it iter.Seq2[*K, error]) ([]K, error)

TryCollectDeref collects dereferenced non-error values in a slice. If an error is encountered, the slice and encountered error are returned immediately.

func TryCollectDerefWithCap

func TryCollectDerefWithCap[K any](it iter.Seq2[*K, error], cap int) ([]K, error)

TryCollectDerefWithCap collects dereferenced non-error values in a slice with the given capacity. If an error is encountered, the slice and encountered error are returned immediately.

func TryCollectWithCap

func TryCollectWithCap[K any](it iter.Seq2[K, error], cap int) ([]K, error)

TryCollectWithCap collects non-error values in a slice with the given capacity. If an error is encountered, the slice and encountered error are returned immediately.

func TryFilter

func TryFilter[K any](seq iter.Seq2[K, error], f func(K) bool) iter.Seq2[K, error]

TryFilter filters non-error tuples with the given predicate function. For error-tuples, the filter is not applied and yield is called with the error and the value.

func TryFilterErr

func TryFilterErr[K any](seq iter.Seq2[K, error], f func(K) (bool, error)) iter.Seq2[K, error]

TryFilterErr filters non-error tuples with the given predicate function. For error-tuples, the filter is not applied and yield is called with the error and the value. If the predicate function errors, the value it errored for and the error is yielded.

func TryFlatmap

func TryFlatmap[In, Out any](seq iter.Seq2[In, error], f func(In) iter.Seq[Out]) iter.Seq2[Out, error]

TryFlatmap maps non-error tuples with the given function, flattening the result. For error-tuples, the function is not applied and yield is called with the error and the zero Out value.

func TryFlatmapErr

func TryFlatmapErr[In, Out any](seq iter.Seq2[In, error], f func(In) iter.Seq2[Out, error]) iter.Seq2[Out, error]

TryFlatmapErr maps non-error tuples with the given function, flattening the result. For error-tuples, the function is not applied and yield is called with the error and the zero Out value.

func TryMap

func TryMap[In, Out any](seq iter.Seq2[In, error], f func(In) Out) iter.Seq2[Out, error]

TryMap maps non-error tuples with the given function. For error-tuples, the function is not applied and yield is called with the error and the zero Out value.

func TryMapErr

func TryMapErr[In, Out any](seq iter.Seq2[In, error], f func(In) (Out, error)) iter.Seq2[Out, error]

TryMapErr applies the transformation function f to each non-error tuple of the given sequence. For error-tuples, the function is not applied and yield is called with the error and the zero value of Out.

func TryTap

func TryTap[K any](seq iter.Seq2[K, error], f func(K)) iter.Seq2[K, error]

TryTap calls the given function on each non-error tuple value.

func Unzip

func Unzip[V1, V2 any](seq iter.Seq[Zipped[V1, V2]]) (iter.Seq[V1], iter.Seq[V2], func())

func Unzip2

func Unzip2[K1, V1, K2, V2 any](seq iter.Seq[Zipped2[K1, V1, K2, V2]]) (iter.Seq2[K1, V1], iter.Seq2[K2, V2], func())

func Values

func Values[K, V any](seq iter.Seq2[K, V]) iter.Seq[V]

func Zip

func Zip[V1, V2 any](seq1 iter.Seq[V1], seq2 iter.Seq[V2]) iter.Seq[Zipped[V1, V2]]

Zip combines two sequences into a single sequence by yielding zipped pairs of elements from each sequence. The returned sequence will produce a Zipped struct for each pair of elements, containing the value and presence flag for each sequence. The sequences will be zipped until both sequences are exhausted or the yield function returns false. If the yield function returns false, the zipping will stop and the function will return. After the first sequence is exhausted, the remaining elements of the second sequence will be paired with default values and presence flags.

func Zip2

func Zip2[K1, V1, K2, V2 any](seq1 iter.Seq2[K1, V1], seq2 iter.Seq2[K2, V2]) iter.Seq[Zipped2[K1, V1, K2, V2]]

Zip2 combines two sequences into a single sequence by yielding zipped pairs of elements from each sequence. The returned sequence will produce a Zipped2 struct for each pair of elements, containing the value and presence flag for each sequence. The sequences will be zipped until both sequences are exhausted or the yield function returns false. If the yield function returns false, the zipping will stop and the function will return. After the first sequence is exhausted, the remaining elements of the second sequence will be paired with default values and presence flags.

Types

type Zipped

type Zipped[V1, V2 any] struct {
	V1  V1
	Ok1 bool

	V2  V2
	Ok2 bool
}

Zipped represents a type that holds two values of types V1 and V2 respectively, along with boolean flags indicating whether the values are present (Ok1 and Ok2).

type Zipped2

type Zipped2[K1, V1, K2, V2 any] struct {
	K1  K1
	V1  V1
	Ok1 bool

	K2  K2
	V2  V2
	Ok2 bool
}

Zipped2 represents a type that holds two values of types K1, V1 and K2, V2 respectively, along with boolean flags indicating whether the values are present (Ok1 and Ok2).

Jump to

Keyboard shortcuts

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