fun

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package fun provides reusable general-purpose functions (Const, Swap, Curry) and data structures (Unit, Pair, Either).

Index

Constants

This section is empty.

Variables

View Source
var Unit1 = Unit{}

Unit1 is the value of type Unit.

Functions

func Cast added in v0.2.3

func Cast[A any](i Any) (a A, err error)

Cast converts interface {} to ordinary type A. It'a simple operation i.(A) represented as a function. In case the conversion is not possible, returns an error.

func Compose added in v0.1.4

func Compose[A any, B any, C any](f func(A) B, g func(B) C) func(A) C

Compose executes the given functions in sequence.

func Const

func Const[A any, B any](b B) func(A) B

Const creates a function that will ignore it's input and return the specified value.

func ConstUnit

func ConstUnit[B any](b B) func(Unit) B

ConstUnit creates a function that will ignore it's Unit input and return the specified value.

func Curry

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

Curry takes a function that has two arguments and returns a function with two argument lists.

func Identity added in v0.0.11

func Identity[A any](a A) A

Identity function returns the given value unchanged.

func Nothing added in v0.2.1

func Nothing[A any]() A

Nothing panics. However, can be used anywhere where type A is needed.

func PairV1 added in v0.3.0

func PairV1[A any, B any](p Pair[A, B]) A

PairV1 returns the first element of the pair.

func PairV2 added in v0.3.0

func PairV2[A any, B any](p Pair[A, B]) B

PairV2 returns the second element of the pair.

func RecoverToErrorVar added in v0.2.3

func RecoverToErrorVar(name string, err *error)

RecoverToErrorVar recovers and places the recovered error into the given variable

func RecoverToLog added in v0.2.3

func RecoverToLog(name string)

RecoverToLog in case of error just logs it.

func Swap

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

Swap returns a curried function with swapped order of arguments.

func ToString added in v0.1.0

func ToString[A any](a A) string

ToString converts the value to string using `Sprintf` `%v`.

func UnsafeCast added in v0.2.0

func UnsafeCast[A any](i Any) A

UnsafeCast converts interface {} to ordinary type A. It'a simple operation i.(A) represented as a function. In case the conversion is not possible throws a panic.

Types

type Any added in v0.2.3

type Any interface{}

func CastAsInterface added in v0.2.0

func CastAsInterface[A any](a A) Any

CastAsInterface casts a value of an arbitrary type as interface {}.

type Pair added in v0.0.11

type Pair[A any, B any] struct {
	V1 A
	V2 B
}

Pair is a data structure that has two values.

func NewPair added in v0.0.11

func NewPair[A any, B any](a A, b B) Pair[A, B]

NewPair constructs the pair.

type Unit

type Unit struct{}

Unit is a type that has only a single value.

Jump to

Keyboard shortcuts

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