f

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2024 License: BSD-3-Clause Imports: 4 Imported by: 1

Documentation

Overview

Package f is a golang library implementing some basic algorithms.

The canonical import for this library is go.bug.st/f:

import "go.bug.st/f"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert added in v0.3.0

func Assert(condition bool, msg string, args ...any)

Assert panics if condition is false.

func DiscardCh

func DiscardCh[T any](ch <-chan T)

DiscardCh consumes all incoming messages from the given channel until it's closed.

func Filter

func Filter[T any](values []T, matcher Matcher[T]) []T

Filter takes a slice of type []T and a Matcher[T]. It returns a newly allocated slice containing only those elements of the input slice that satisfy the matcher.

func Map

func Map[T, U any](values []T, mapper Mapper[T, U]) []U

Map applies the Mapper function to each element of the slice and returns a new slice with the results in the same order.

func Must

func Must[T any](val T, err error) T

Must should be used to wrap a call to a function returning a value and an error. Must returns the value if the errors is nil, or panics otherwise.

func ParallelMap

func ParallelMap[T, U any](values []T, mapper Mapper[T, U], jobs ...int) []U

ParallelMap applies the Mapper function to each element of the slice and returns a new slice with the results in the same order. This is executed among multilple goroutines in parallel. If jobs is specified it will indicate the maximum number of goroutines to be spawned.

func Ptr added in v0.3.0

func Ptr[T any](v T) *T

Ptr returns a pointer to v.

func Reduce

func Reduce[T any](values []T, reducer Reducer[T], initialValue ...T) T

Reduce applies the Reducer function to all elements of the input values and returns the result.

func Uniq

func Uniq[T comparable](in []T) []T

Uniq return a copy of the input array with all duplicates removed

func UnwrapOrDefault added in v0.3.0

func UnwrapOrDefault[T any](ptr *T) T

UnwrapOrDefault returns the ptr value if it is not nil, otherwise returns the zero value.

Types

type Future

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

Future is an object that holds a result value. The value may be read and written asynchronously.

func (*Future[T]) Await

func (f *Future[T]) Await() T

Await for a result from the Future, blocks until a result is available.

func (*Future[T]) Send

func (f *Future[T]) Send(value T)

Send a result in the Future. Threads waiting for result will be unlocked.

type Mapper

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

Mapper is a function that converts a value of one type to another type.

type Matcher

type Matcher[T any] func(T) bool

Matcher is a function that tests if a given value matches a certain criteria.

func Equals

func Equals[T comparable](value T) Matcher[T]

Equals return a Matcher that matches the given value

func NotEquals

func NotEquals[T comparable](value T) Matcher[T]

NotEquals return a Matcher that does not match the given value

type Reducer

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

Reducer is a function that combines two values of the same type and return the combined value.

Jump to

Keyboard shortcuts

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