fn

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// DefaultQueueSize is the default size to use for concurrent queues.
	DefaultQueueSize = 10
)

Variables

This section is empty.

Functions

func All added in v1.0.5

func All[A any](pred func(A) bool, s []A) bool

All returns true when the supplied predicate evaluates to true for all of the values in the slice.

func Any added in v1.0.5

func Any[A any](pred func(A) bool, s []A) bool

Any returns true when the supplied predicate evaluates to true for any of the values in the slice.

func ChainOption

func ChainOption[A, B any](f func(A) Option[B]) func(Option[A]) Option[B]

ChainOption transforms a function A -> Option[B] into one that accepts an Option[A] as an argument.

ChainOption : (A -> Option[B]) -> Option[A] -> Option[B].

func ElimOption

func ElimOption[A, B any](o Option[A], b func() B, f func(A) B) B

ElimOption is the universal Option eliminator. It can be used to safely handle all possible values inside the Option by supplying two continuations.

ElimOption : (Option[A], () -> B, A -> B) -> B.

func Filter added in v1.0.5

func Filter[A any](pred func(A) bool, s []A) []A

Filter creates a new slice of values where all the members of the returned slice pass the predicate that is supplied in the argument.

func Flatten added in v1.0.5

func Flatten[A any](s [][]A) []A

Flatten takes a slice of slices and returns a concatenation of those slices.

func Foldl added in v1.0.5

func Foldl[A, B any](f func(B, A) B, seed B, s []A) B

Foldl iterates through all members of the slice left to right and reduces them pairwise with an accumulator value that is seeded with the seed value in the argument.

func Foldr added in v1.0.5

func Foldr[A, B any](f func(A, B) B, seed B, s []A) B

Foldr, is exactly like Foldl except that it iterates over the slice from right to left.

func HasDuplicates added in v1.0.7

func HasDuplicates[A comparable](items []A) bool

HasDuplicates checks if the given slice contains any duplicate elements. It returns false if there are no duplicates in the slice (i.e., all elements are unique), otherwise returns false.

func LiftA2Option

func LiftA2Option[A, B, C any](
	f func(A, B) C,
) func(Option[A], Option[B]) Option[C]

LiftA2Option transforms a pure function (A, B) -> C into one that will operate in an Option context. For the returned function, if either of its arguments are None, then the result will be None.

LiftA2Option : ((A, B) -> C) -> (Option[A], Option[B]) -> Option[C].

func Map added in v1.0.5

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

Map applies the function argument to all members of the slice and returns a slice of those return values.

func MapLeft added in v1.0.2

func MapLeft[L any, R any, O any](f func(L) O) func(Either[L, R]) Option[O]

MapLeft maps the left value of the Either to a new value.

func MapOption

func MapOption[A, B any](f func(A) B) func(Option[A]) Option[B]

MapOption transforms a pure function A -> B into one that will operate inside the Option context.

MapOption : (A -> B) -> Option[A] -> Option[B].

func MapOptionZ added in v1.0.2

func MapOptionZ[A, B any](o Option[A], f func(A) B) B

MapOptionZ transforms a pure function A -> B into one that will operate inside the Option context. Unlike MapOption, this function will return the default/zero argument of the return type if the Option is empty.

func NewSubMap added in v1.0.6

func NewSubMap[K comparable, V any](m map[K]V, keys []K) (map[K]V, error)

NewSubMap creates a sub-map from a given map using specified keys. It errors if any of the keys is not found in the map.

func NewSubMapIntersect added in v1.0.6

func NewSubMapIntersect[K comparable, V any](m map[K]V, keys []K) map[K]V

NewSubMapIntersect returns a sub-map of `m` containing only the keys found in both `m` and the `keys` slice.

func RecvOrTimeout added in v1.0.2

func RecvOrTimeout[T any](c <-chan T, timeout time.Duration) (T, error)

RecvOrTimeout attempts to recv over chan c, returning the value. If the timeout passes before the recv succeeds, an error is returned

func RecvResp added in v1.0.2

func RecvResp[T any](r <-chan T, e <-chan error, q <-chan struct{}) (T, error)

RecvResp takes three channels: a response channel, an error channel and a quit channel. If either of these channels are sent on, then the function will exit with that response. This can be used to wait for a response, error, or a quit signal.

func Reduce added in v1.0.2

func Reduce[T any, V any, S []V](s S, f Reducer[T, V]) T

Reduce takes a slice of something, and a reducer, and produces a final accumulated value.

func Replicate added in v1.0.5

func Replicate[A any](n uint, val A) []A

Replicate generates a slice of values initialized by the prototype value.

func SendOrQuit added in v1.0.2

func SendOrQuit[T any, Q any](c chan<- T, msg T, quit chan Q) bool

SendOrQuit attempts to and a message through channel c. If this succeeds, then bool is returned. Otherwise if a quit signal is received first, then false is returned.

func SetDiff added in v1.0.2

func SetDiff[T comparable](a, b []T) []T

SetDiff returns all the items that are in the first set but not in the second.

func SliceToMap added in v1.0.6

func SliceToMap[A any, K comparable, V any](s []A, keyFunc func(A) K,
	valueFunc func(A) V) map[K]V

SliceToMap converts a slice to a map using the provided key and value functions.

func Span added in v1.0.5

func Span[A any](pred func(A) bool, s []A) ([]A, []A)

Span, applied to a predicate and a slice, returns two slices where the first element is the longest prefix (possibly empty) of slice elements that satisfy the predicate and second element is the remainder of the slice.

func SplitAt added in v1.0.5

func SplitAt[A any](n uint, s []A) ([]A, []A)

SplitAt(n, s) returns a tuple where first element is s prefix of length n and second element is the remainder of the list.

func Sum added in v1.0.6

func Sum[B Number](items []B) B

Sum calculates the sum of a slice of numbers, `items`.

func ZipWith added in v1.0.5

func ZipWith[A, B, C any](f func(A, B) C, a []A, b []B) []C

ZipWith combines slice elements with the same index using the function argument, returning a slice of the results.

Types

type ConcurrentQueue added in v1.0.2

type ConcurrentQueue[T any] struct {
	// contains filtered or unexported fields
}

ConcurrentQueue is a typed concurrent-safe FIFO queue with unbounded capacity. Clients interact with the queue by pushing items into the in channel and popping items from the out channel. There is a goroutine that manages moving items from the in channel to the out channel in the correct order that must be started by calling Start().

func NewConcurrentQueue added in v1.0.2

func NewConcurrentQueue[T any](bufferSize int) *ConcurrentQueue[T]

NewConcurrentQueue constructs a ConcurrentQueue. The bufferSize parameter is the capacity of the output channel. When the size of the queue is below this threshold, pushes do n[?12;4$yot incur the overhead of the less efficient overflow structure.

func (*ConcurrentQueue[T]) ChanIn added in v1.0.2

func (cq *ConcurrentQueue[T]) ChanIn() chan<- T

ChanIn returns a channel that can be used to push new items into the queue.

func (*ConcurrentQueue[T]) ChanOut added in v1.0.2

func (cq *ConcurrentQueue[T]) ChanOut() <-chan T

ChanOut returns a channel that can be used to pop items from the queue.

func (*ConcurrentQueue[T]) Start added in v1.0.2

func (cq *ConcurrentQueue[T]) Start()

Start begins a goroutine that manages moving items from the in channel to the out channel. The queue tries to move items directly to the out channel minimize overhead, but if the out channel is full it pushes items to an overflow queue. This must be called before using the queue.

func (*ConcurrentQueue[T]) Stop added in v1.0.2

func (cq *ConcurrentQueue[T]) Stop()

Stop ends the goroutine that moves items from the in channel to the out channel. This does not clear the queue state, so the queue can be restarted without dropping items.

type Either added in v1.0.2

type Either[L any, R any] struct {
	// contains filtered or unexported fields
}

Either is a type that can be either left or right.

func NewLeft added in v1.0.2

func NewLeft[L any, R any](l L) Either[L, R]

NewLeft returns an Either with a left value.

func NewRight added in v1.0.2

func NewRight[L any, R any](r R) Either[L, R]

NewRight returns an Either with a right value.

func (Either[L, R]) IsLeft added in v1.0.2

func (e Either[L, R]) IsLeft() bool

IsLeft returns true if the Either is left.

func (Either[L, R]) IsRight added in v1.0.2

func (e Either[L, R]) IsRight() bool

IsRight returns true if the Either is right.

func (Either[L, R]) WhenLeft added in v1.0.2

func (e Either[L, R]) WhenLeft(f func(L))

WhenLeft executes the given function if the Either is left.

func (Either[L, R]) WhenRight added in v1.0.2

func (e Either[L, R]) WhenRight(f func(R))

WhenRight executes the given function if the Either is right.

type Event added in v1.0.2

type Event interface {
	Timestamp() time.Time
}

Event is a generic event that can be sent to a subscriber.

type EventDistributor added in v1.0.2

type EventDistributor[T any] struct {
	// contains filtered or unexported fields
}

EventDistributor is a struct type that helps to distribute events to multiple subscribers.

func NewEventDistributor added in v1.0.2

func NewEventDistributor[T any]() *EventDistributor[T]

NewEventDistributor creates a new event distributor of the declared type.

func (*EventDistributor[T]) NotifySubscribers added in v1.0.2

func (d *EventDistributor[T]) NotifySubscribers(events ...T)

NotifySubscribers sends the given events to all subscribers.

func (*EventDistributor[T]) RegisterSubscriber added in v1.0.2

func (d *EventDistributor[T]) RegisterSubscriber(subscriber *EventReceiver[T])

RegisterSubscriber adds a new subscriber for receiving events.

func (*EventDistributor[T]) RemoveSubscriber added in v1.0.2

func (d *EventDistributor[T]) RemoveSubscriber(
	subscriber *EventReceiver[T]) error

RemoveSubscriber removes the given subscriber and also stops it from processing events.

type EventPublisher added in v1.0.2

type EventPublisher[T any, Q any] interface {
	// RegisterSubscriber adds a new subscriber for receiving events. The
	// deliverExisting boolean indicates whether already existing items
	// should be sent to the NewItemCreated channel when the subscription is
	// started. An optional deliverFrom can be specified to indicate from
	// which timestamp/index/marker onward existing items should be
	// delivered on startup. If deliverFrom is nil/zero/empty then all
	// existing items will be delivered.
	RegisterSubscriber(receiver *EventReceiver[T], deliverExisting bool,
		deliverFrom Q) error

	// RemoveSubscriber removes the given subscriber and also stops it from
	// processing events.
	RemoveSubscriber(subscriber *EventReceiver[T]) error
}

EventPublisher is an interface type for a component that offers event based subscriptions for publishing events.

type EventReceiver added in v1.0.2

type EventReceiver[T any] struct {

	// NewItemCreated is sent to when a new item was created successfully.
	NewItemCreated *ConcurrentQueue[T]

	// ItemRemoved is sent to when an existing item was removed.
	ItemRemoved *ConcurrentQueue[T]
	// contains filtered or unexported fields
}

EventReceiver is a struct type that holds two queues for new and removed items respectively.

func NewEventReceiver added in v1.0.2

func NewEventReceiver[T any](queueSize int) *EventReceiver[T]

NewEventReceiver creates a new event receiver with concurrent queues of the given size.

func (*EventReceiver[T]) ID added in v1.0.2

func (e *EventReceiver[T]) ID() uint64

ID returns the internal process-unique ID of the subscription.

func (*EventReceiver[T]) Stop added in v1.0.2

func (e *EventReceiver[T]) Stop()

Stop stops the receiver from processing events.

type Number added in v1.0.6

type Number interface {
	constraints.Integer | constraints.Float | constraints.Complex
}

Number is a type constraint for all numeric types in Go (integers, float and complex numbers)

type Option

type Option[A any] struct {
	// contains filtered or unexported fields
}

Option[A] represents a value which may or may not be there. This is very often preferable to nil-able pointers.

func Find added in v1.0.5

func Find[A any](pred func(A) bool, s []A) Option[A]

Find returns the first value that passes the supplied predicate, or None if the value wasn't found.

func FlattenOption

func FlattenOption[A any](oo Option[Option[A]]) Option[A]

FlattenOption joins multiple layers of Options together such that if any of the layers is None, then the joined value is None. Otherwise the innermost Some value is returned.

FlattenOption : Option[Option[A]] -> Option[A].

func None

func None[A any]() Option[A]

None trivially constructs an empty option

None : Option[A].

func Some

func Some[A any](a A) Option[A]

Some trivially injects a value into an optional context.

Some : A -> Option[A].

func (Option[A]) Alt

func (o Option[A]) Alt(o2 Option[A]) Option[A]

Alt chooses the left Option if it is full, otherwise it chooses the right option. This can be useful in a long chain if you want to choose between many different ways of producing the needed value.

Alt : Option[A] -> Option[A] -> Option[A].

func (Option[A]) IsNone

func (o Option[A]) IsNone() bool

IsNone returns true if the Option is empty

IsNone : Option[A] -> bool.

func (Option[A]) IsSome

func (o Option[A]) IsSome() bool

IsSome returns true if the Option contains a value

IsSome : Option[A] -> bool.

func (Option[A]) UnsafeFromSome added in v1.0.1

func (o Option[A]) UnsafeFromSome() A

UnsafeFromSome can be used to extract the internal value. This will panic if the value is None() though.

func (Option[A]) UnwrapOr

func (o Option[A]) UnwrapOr(a A) A

UnwrapOr is used to extract a value from an option, and we supply the default value in the case when the Option is empty.

UnwrapOr : (Option[A], A) -> A.

func (Option[A]) UnwrapOrErr added in v1.0.4

func (o Option[A]) UnwrapOrErr(err error) (A, error)

UnwrapOrErr is used to extract a value from an option, if the option is empty, then the specified error is returned directly.

func (Option[A]) UnwrapOrFail added in v1.0.4

func (o Option[A]) UnwrapOrFail(t *testing.T) A

UnwrapOrFail is used to extract a value from an option within a test context. If the option is None, then the test fails.

func (Option[A]) UnwrapOrFunc added in v1.0.2

func (o Option[A]) UnwrapOrFunc(f func() A) A

UnwrapOrFunc is used to extract a value from an option, and we supply a thunk to be evaluated in the case when the Option is empty.

func (Option[A]) UnwrapOrFuncErr added in v1.0.2

func (o Option[A]) UnwrapOrFuncErr(f func() (A, error)) (A, error)

UnwrapOrFuncErr is used to extract a value from an option, and we supply a thunk to be evaluated in the case when the Option is empty.

func (Option[A]) WhenSome

func (o Option[A]) WhenSome(f func(A))

WhenSome is used to conditionally perform a side-effecting function that accepts a value of the type that parameterizes the option. If this function performs no side effects, WhenSome is useless.

WhenSome : (Option[A], A -> ()) -> ().

type Queue added in v1.0.2

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue is a generic queue implementation.

func NewQueue added in v1.0.2

func NewQueue[T any](startingItems ...T) Queue[T]

NewQueue creates a new Queue.

func (*Queue[T]) Dequeue added in v1.0.2

func (q *Queue[T]) Dequeue() Option[T]

Dequeue removes an element from the front of the Queue. If there're no items in the queue, then None is returned.

func (*Queue[T]) Enqueue added in v1.0.2

func (q *Queue[T]) Enqueue(value ...T)

Enqueue adds one or more an items to the end of the Queue.

func (*Queue[T]) IsEmpty added in v1.0.2

func (q *Queue[T]) IsEmpty() bool

IsEmpty returns true if the Queue is empty

func (*Queue[T]) Peek added in v1.0.2

func (q *Queue[T]) Peek() Option[T]

Peek returns the first item in the queue without removing it. If the queue is empty, then None is returned.

func (*Queue[T]) Size added in v1.0.2

func (q *Queue[T]) Size() int

Size returns the number of items in the Queue

type Reducer added in v1.0.2

type Reducer[T, V any] func(accum T, value V) T

Reducer represents a function that takes an accumulator and the value, then returns a new accumulator.

type Set added in v1.0.2

type Set[T comparable] map[T]struct{}

Set is a generic set using type params that supports the following operations: diff, union, intersection, and subset.

func KeySet added in v1.0.6

func KeySet[K comparable, V any](m map[K]V) Set[K]

KeySet converts a map into a Set containing the keys of the map.

func NewSet added in v1.0.2

func NewSet[T comparable](elems ...T) Set[T]

NewSet returns a new set with the given elements.

func (Set[T]) Add added in v1.0.2

func (s Set[T]) Add(e T)

Add adds an element to the set.

func (Set[T]) Contains added in v1.0.2

func (s Set[T]) Contains(e T) bool

Contains returns true if the set contains the element.

func (Set[T]) Diff added in v1.0.2

func (s Set[T]) Diff(other Set[T]) Set[T]

Diff returns the difference between two sets.

func (Set[T]) Equal added in v1.0.2

func (s Set[T]) Equal(other Set[T]) bool

Equal returns true if the set is equal to the other set.

func (Set[T]) Intersect added in v1.0.2

func (s Set[T]) Intersect(other Set[T]) Set[T]

Intersect returns the intersection of two sets.

func (Set[T]) Remove added in v1.0.2

func (s Set[T]) Remove(e T)

Remove removes an element from the set.

func (Set[T]) Subset added in v1.0.2

func (s Set[T]) Subset(other Set[T]) bool

Subset returns true if the set is a subset of the other set.

func (Set[T]) ToSlice added in v1.0.2

func (s Set[T]) ToSlice() []T

ToSlice returns the set as a slice.

func (Set[T]) Union added in v1.0.2

func (s Set[T]) Union(other Set[T]) Set[T]

Union returns the union of two sets.

Jump to

Keyboard shortcuts

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