collections

package
v0.0.0-...-e865158 Latest Latest
Warning

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

Go to latest
Published: May 18, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// UIntSizeEncodingLength is the number of bits required to encode the size of a uint.
	UIntSizeEncodingLength = 5 + (^uint(0) >> 63)

	// UIntSize is the size of a uint in bits.
	UIntSize = 1 << UIntSizeEncodingLength
)

Variables

This section is empty.

Functions

func Channel

func Channel[T any](i Iterable[T]) <-chan T

func Collect

func Collect[T, U any](s Stream[T], c Collector[T, U]) U

func Filter

func Filter[E any](s []E, f Predicate[E]) []E

Filter returns a new slice containing all elements from `s` for which `f` evaluates to `true`.

func Find

func Find[E any](s []E, f Predicate[E]) *E

Find returns a poiner to the first element from `s` for which `f` evaluates to `true` or `nil` if no such element exists.

func Fold

func Fold[A, B any](s []A, a B, f Reducer[A, B]) B

Fold applies `f` to each element of slice `s` and the current value of `a` and returns the final value of `a`.

func Map

func Map[A, B any](s []A, f Function[A, B]) []B

Map applies `f` to each element of slice `s` and returns a new slice containing the results.

func Reduce

func Reduce[E any](s []E, f Reducer[E, E]) E

Reduce operates like Fold, but uses `s[0]` as the initial accumulator value and then folds `s[1:]`. If `s` is empty, Reduce panics.

func Sample

func Sample[E any](s []E, f Sampler) []E

Sample returns a new slice containing all elements from `s` whose index satisfies `f`.

Note that while Sample resembles Filter in that it returns a new slice containing a subset of the elements from `s`, Sample only considers the index of each element, whereas Filter considers the element's value itself.

func SliceCollector

func SliceCollector[T any](s Stream[T]) []T

Types

type BitSet

type BitSet []uint

func NewBitSet

func NewBitSet(cap int) BitSet

func (BitSet) Capacity

func (bits BitSet) Capacity() int

func (BitSet) IsSet

func (bits BitSet) IsSet(i int) bool

func (BitSet) Iterator

func (bits BitSet) Iterator() (Iterator[*int], *int)

func (BitSet) Set

func (bits BitSet) Set(i int)

func (BitSet) Unset

func (bits BitSet) Unset(i int)

type Collection

type Collection[T any] []T

func (Collection[T]) Filter

func (c Collection[T]) Filter(f Predicate[T]) Collection[T]

func (Collection[T]) Find

func (c Collection[T]) Find(f Predicate[T]) *T

func (Collection[T]) Fold

func (c Collection[T]) Fold(a T, f Reducer[T, T]) T

func (Collection[T]) Iterator

func (c Collection[T]) Iterator() (Iterator[T], *T)

func (Collection[T]) Map

func (c Collection[T]) Map(f Function[T, T]) Collection[T]

func (Collection[T]) Reduce

func (c Collection[T]) Reduce(f Reducer[T, T]) T

type CollectionError

type CollectionError uint
const (

	// ErrEmptySliceParameter occurs when a function is called with an empty slice parameter where a non-empty slice is required.
	ErrEmptySliceParameter CollectionError

	// ErrNotEnoughParameters occurs when Range is called with no parameters.
	ErrNotEnoughParameters

	// ErrTooManyParameters occurs when Range is called with more than three parameters.
	ErrTooManyParameters

	// ErrRangeStepZero occurs when Range is called with a step size of zero.
	ErrRangeStepIsZero

	// ErrEmptyStream occurs when an empty stream is passed where a non-empty stream is required.
	ErrEmptyStream

	// ErrSliceRangeOutOfBounds occurs when either end of a sub-slice is outside the source slide's range.
	ErrSliceRangeOutOfBounds

	// ErrIndexOutOfBounds occurs when an accessed index refers to memory outside the structure's allocated memory.
	ErrIndexOutOfBounds

	// ErrNegativeCapacity occurs when a negative capacity is passed to a function that requires a non-negative capacity.
	ErrNegativeCapacity
)

func (CollectionError) Error

func (e CollectionError) Error() string

type Collector

type Collector[T, U any] Function[Stream[T], U]

func FoldCollector

func FoldCollector[T, U any](a U, f Reducer[*T, U]) Collector[T, U]

func ReduceCollector

func ReduceCollector[T any](f Reducer[*T, T]) Collector[T, T]

type Float

type Float = constraints.Float

type Function

type Function[T, U any] func(T) U

A Function is a function that transforms a value.

type Generator

type Generator[T any] func(yield Yield[T])

func (Generator[T]) Iterator

func (g Generator[T]) Iterator() (IterableIterator[*T], *T)

func (Generator[T]) Stream

func (g Generator[T]) Stream() Stream[T]

type Integer

type Integer = constraints.Integer

type Iterable

type Iterable[T any] interface {
	Iterator() (Iterator[T], T)
}

type IterableIterator

type IterableIterator[T any] interface {
	Iterator[T]
	Iterable[T]
}

func Range

func Range[T Number](options ...T) IterableIterator[*T]

type Iterator

type Iterator[T any] interface {
	Next() bool
}

type JoinPredicate

type JoinPredicate[T, U any] func(*T, *U) bool

A JoinPredicate is a function that checks whether two values satisfy some condition.

type Monitor

type Monitor chan struct{}

A Monitor is a channel that can be used to synchronize goroutines.

func NewMonitor

func NewMonitor() Monitor

NewMonitor creates a new monitor.

func (Monitor) Close

func (m Monitor) Close()

Close closes the monitor and notifies all goroutines waiting on the monitor.

func (Monitor) Notify

func (m Monitor) Notify()

Notify notifies the goroutine waiting on the monitor, if any.

func (Monitor) Wait

func (m Monitor) Wait() bool

Wait waits for a notification on the monitor. Returns false if the monitor is closed.

type Number

type Number interface{ Integer | Float }

type Pair

type Pair[T, U any] struct {
	First  T
	Second U
}

A Pair is just what its name suggests

func CartesianProduct

func CartesianProduct[T, U any](s []T, t []U) []Pair[T, U]

CartesianProduct returns an Iterable that iterates over all pairs of elements from `s` and `t`. It works like Join with a condition that always evaluates to `true`, but is faster because the condition is not actually evaluated.

func Join

func Join[T, U any](s []T, t []U, f JoinPredicate[T, U]) []Pair[T, U]

Join returns an Iterable that iterates over all pairs of elements from `s` and `t` for which `f` evaluates to `true`.

func NewPair

func NewPair[T, U any](first T, second U) *Pair[T, U]

NewPair returns a pointer to a new pair.

func (*Pair[T, U]) String

func (p *Pair[T, U]) String() string

String returns a string representation of the pair.

func (*Pair[T, U]) Unpack

func (p *Pair[T, U]) Unpack() (T, U)

Unpack returns the elements of the pair.

func (*Pair[T, U]) Value

func (p *Pair[T, U]) Value() Pair[T, U]

Value returns a copy of the pair.

type Predicate

type Predicate[T any] func(T) bool

A Predicate is a function that checks whether a value satisfies some condition.

type Reducer

type Reducer[T, U any] func(U, T) U

A Reducer is a function that combines two values into one.

type Reference

type Reference[T any] interface {
	Value() T
}

type ReferencePair

type ReferencePair[T, U any] struct {
	Pair[*T, *U]
}

A ReferencePair behaves like a Pair[*T, *U] but with its elements being dereferenced before access.

func NewReferencePair

func NewReferencePair[T, U any](first *T, second *U) *ReferencePair[T, U]

func (*ReferencePair[T, U]) String

func (p *ReferencePair[T, U]) String() string

String returns a string representation of the pair.

func (*ReferencePair[T, U]) Unpack

func (p *ReferencePair[T, U]) Unpack() (T, U)

Unpack returns the dereferenced elements of the pair.

func (*ReferencePair[T, U]) Value

func (p *ReferencePair[T, U]) Value() Pair[T, U]

Value returns a copy of the pair with its elements dereferenced.

type Sampler

type Sampler func(int) bool

A Sampler is a function that checks whether a value's index satisfies some condition.

type Stream

type Stream[T any] interface {
	Read() *T
}

func FromInterfaceIterable

func FromInterfaceIterable[T any](it Iterable[*T]) Stream[T]

func FromReferenceIterable

func FromReferenceIterable[T any](it Iterable[Reference[T]]) Stream[T]

func FromValueIterable

func FromValueIterable[T any](it Iterable[*T]) Stream[T]

func Transform

func Transform[T, U any](s Stream[T], t Transformer[T, U]) Stream[U]

type Streamer

type Streamer[T any] interface {
	Stream() Stream[T]
}

type Transformer

type Transformer[T, U any] Function[Stream[T], Stream[U]]

func FilterStream

func FilterStream[T any](f Predicate[*T]) Transformer[T, T]

func FoldStream

func FoldStream[T, U any](a U, f Reducer[*T, U]) Transformer[T, U]

func MapStream

func MapStream[T, U any](f Function[*T, U]) Transformer[T, U]

func ReduceStream

func ReduceStream[T any](f Reducer[*T, T]) Transformer[T, T]

func SampleStream

func SampleStream[T any](f Predicate[int]) Transformer[T, T]

type ValuePair

type ValuePair[T, U any] struct {
	Pair[T, U]
}

A ValuePair is an alias for Pair[T, U] that exists for the sake of naming consistency.

func NewValuePair

func NewValuePair[T, U any](first T, second U) *ValuePair[T, U]

type Yield

type Yield[T any] func(*T)

Jump to

Keyboard shortcuts

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