fn

package
v0.0.0-...-be83087 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package fn contains the sort of non-Go-like, occasionally higher-order, utility functions you might find in a functional language.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[S ~[]E, F ~func(E) bool, E any](s S, f F) bool

All returns true if the function f returns true for all elements of slice s. Returns true for an empty slice.

func Any

func Any[S ~[]E, F ~func(E) bool, E any](s S, f F) bool

Any returns true if the function f returns true for at least one element of slice s. Returns false for an empty slice.

func CountIf

func CountIf[S ~[]E, F ~func(E) bool, E any](s S, f F) (count int)

CountIf returns the number of items in a slice that satisfy a predicate.

func Filter

func Filter[S ~[]E, P ~func(E) bool, E any](s S, p P) (out []E)

Filter returns a new slice that contains just the elements matching a predicate.

func ForEach

func ForEach[S ~[]E, F ~func(E), E any](s S, f F)

ForEach invokes a function (which must not return anything) for each element of a slice in order.

func Head[S ~[]E, E any](s S, def E) E

Head returns the first line of a slice, or a default value for an empty slice.

func If

func If[T any](c bool, t, f T) T

If is an expression conditional, similar to the C `?:` operator (except both branches are evaluated).

func Keys

func Keys[M ~map[K]V, K comparable, V any](m M) []K

Keys returns a slice consisting of the keys of a map. The keys are in no particular order, the same as the iteration order of a map.

func Map

func Map[S ~[]I, F ~func(I) O, I, O any](in S, f F) (out []O)

Map returns a new slice that contains the results of applying the given function to each element of the input slice.

func MapE

func MapE[S ~[]I, F ~func(I) (O, error), I, O any](in S, f F) (out []O, err error)

MapE is a variant of Map that allows the function to fail.

func MapRange

func MapRange[F ~func(I) E, I integer, E any](start, end I, f F) (out []E)

MapRange returns a slice with the results of calling function on a range of integers. The range is given in the usual half-open [start, end) style.

func Max

func Max[S ~[]E, E cmp.Ordered](s S) (result E)

Max returns the largest value of a slice of some ordered type.

func MaxF

func MaxF[S ~[]I, F ~func(I) O, I any, O cmp.Ordered](s S, f F) (result O)

MaxF returns the largest result of applying a function to a slice.

func Min

func Min[S ~[]E, E cmp.Ordered](s S) (result E)

Min returns the smallest value of a slice of some ordered type.

func MinF

func MinF[S ~[]I, F ~func(I) O, I any, O cmp.Ordered](s S, f F) (result O)

MinF returns the smallest result of applying a function to a slice.

func Prod

func Prod[S ~[]E, E integer](s S) (result E)

Prod returns the product of a slice of integers. The product of an empty slice is 1.

func ProdF

func ProdF[S ~[]I, F ~func(I) O, I any, O integer](s S, f F) (result O)

ProdF returns the product of the results of applying a function to a slice. The product of an empty slice is 1.

func Sum

func Sum[S ~[]E, E integer](s S) (result E)

Sum returns the sum of a slice of integers. The sum of an empty slice is 0.

func SumF

func SumF[S ~[]I, F ~func(I) O, I any, O integer](s S, f F) (result O)

SumF returns the sum of the results of applying a function to a slice. The sum of an empty slice is 0.

Types

This section is empty.

Jump to

Keyboard shortcuts

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