funcutil

package
v0.4.1-alpha Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package funcutil is an internal package providing utility functions to enable functional programming idioms in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compose

func Compose[T any, S any, R any](f func(T) S, g func(S) R) func(T) R

Compose (f,g) returns a function h: x -> f(g(x))

func Contains

func Contains[T comparable](a []T, x T) bool

Contains returns true when there is some y in slice a such that x == y

func Curry2

func Curry2[T any, S any, R any](f func(T, S) R, x T) func(S) R

Curry2 is for currying functions. with two arguments

func Curry3

func Curry3[T any, S any, R any, Q any](f func(T, S, R) Q, x T) func(S, R) Q

Curry3 is for currying functions. with three arguments

func Exists

func Exists[T any](a []T, f func(T) bool) bool

Exists returns true when there exists some x in slice a such that f(x), otherwise false.

func ExistsInMap

func ExistsInMap[K comparable, T any](a map[K]T, f func(K, T) bool) bool

ExistsInMap returns true when there exists some k,x in map a such that f(k,x), otherwise false.

func First

func First[T any](x T, _ T) T

First returns the first of two arguments

func Iter

func Iter[T any](a []T, f func(T))

Iter iterates over all elements in the slice and call the function on that element.

func Map

func Map[T any, S any](a []T, f func(T) S) []S

Map returns a new slice b such for any i <= len(a), b[i] = f(a[i])

func MapInPlace

func MapInPlace[T any](a []T, f func(T) T)

MapInPlace iterates over all elements in the slice and call the function on that element to update it.

func MapParallel

func MapParallel[T any, S any](a []T, f func(T) S, numRoutines int) []S

MapParallel is a parallel version of Map using numRoutines goroutines.

func MapValues

func MapValues[T comparable, S any](a map[T]S, f func(S) S) map[T]S

MapValues returns a new slice b such for any i <= len(a), b[i] = f(a[i])

func Merge

func Merge[T comparable, S any](a map[T]S, b map[T]S, both func(x S, y S) S)

Merge merges the two maps into the first map. if x is in b but not in a, then a[x] := b[x] if x in both in a and b, then a[x] := both(a[x], b[x]) @mutates a

func Retain

func Retain[T comparable, R any](m map[T]R, keys []T)

Retain removes all the key-value pairs in m where the key is not in keys.

func Reverse

func Reverse[T any](a []T)

Reverse returns the reversed slice

func Second

func Second[T any](_ T, y T) T

Second returns the first of two arguments

func Set

func Set[T comparable](a []T) map[T]struct{}

Set takes an array of elements and returns a set of those elements, represented as a map from elements to empty struct

func SetToOrderedSlice

func SetToOrderedSlice[T constraints.Ordered](set map[T]bool) []T

SetToOrderedSlice converts a set represented as a map from elements to booleans into a slice. Sorts the result in increasing order

func Union

func Union[T comparable](a map[T]bool, b map[T]bool) map[T]bool

Union returns the union of map-represented sets a and b. This mutates map a @mutates a

Types

type Optional

type Optional[T any] interface {
	// ValueOr returns the value of the optional if it is some value, otherwise the default value if it is none
	ValueOr(defaultVal T) T

	// Value returns the value or panics if it is none
	Value() T

	// IsSome returns true if the optional represents some value
	IsSome() bool

	// IsNone returns true is the optional is none
	IsNone() bool
}

An Optional holds a value or none. If it has a value, the IsSome returns true and IsNone returns false.

func BindOption

func BindOption[T any, S any](x Optional[T], f func(T) Optional[S]) Optional[S]

BindOption is the monadic bind operation on optional values

func FindMap

func FindMap[T any, R any](a []T, f func(T) R, p func(R) bool) Optional[R]

FindMap returns Some(f(x)) when there exists some x in slice a such that p(f(x)), otherwise None.

func MapOption

func MapOption[T any, S any](x Optional[T], f func(T) S) Optional[S]

MapOption is the map monadic operation on optional values

func MaybeOr

func MaybeOr[T any](x Optional[T], s Optional[T]) Optional[T]

MaybeOr returns the first optional that is some. If both are none, then the returned value is none

func None

func None[T any]() Optional[T]

None creates an optional value with no value in it

func Some

func Some[T any](x T) Optional[T]

Some creates an optional value with some value in it.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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