lambdas

package
v1.10.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: MIT Imports: 1 Imported by: 0

README

genesis/lambdas

Package lambdas provides helper generic functions.

These functions are especially helpful in combination with other genesis packages.

Documentation

Overview

🛟 Package lambdas provides helper generic functions.

These functions are especially helpful in combination with other `genesis` packages.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T Number](a T) T

Abs returns positive value

Example
package main

import (
	"fmt"

	"github.com/life4/genesis/lambdas"
)

func main() {
	res := lambdas.Abs(-13)
	fmt.Println(res)
}
Output:

13

func Default

func Default[T any](T) T

Default returns the default value of the same type as the given value.

A few examples:

  • 0 for int and float
  • "" for string
  • nil for slice

func DefaultTo

func DefaultTo[T any](def T) func(val T, err error) T

DefaultTo wraps a function invicotaion and returns the specified default value if it returned an error.

Example
package main

import (
	"fmt"

	"github.com/life4/genesis/lambdas"
	"github.com/life4/genesis/slices"
)

func main() {
	res := lambdas.DefaultTo(13)(slices.Min([]int{}))
	fmt.Println(res)
}
Output:

13

func Ensure added in v1.1.0

func Ensure(err error)

Ensure wraps a function invicotaion and panic if it returned an error. Compared to Must, in Ensure the called function only returns an error.

func EqualTo

func EqualTo[T comparable](a T) func(T) bool

EqualTo returns lambda that checks if an item is equal to the given value.

func IsDefault

func IsDefault[T comparable](value T) bool

IsDefault checks if the given value is the default for this type.

A few examples:

  • 0 for int and float
  • "" for string
  • nil for slice

func IsEmpty

func IsEmpty[T any](items []T) bool

IsEmpty checks if the given slice is empty

func IsNaN

func IsNaN[T comparable](value T) bool

func IsNil

func IsNil[T any](value *T) bool

func IsNotDefault

func IsNotDefault[T comparable](value T) bool

IsNotDefault checks if the given value is not the default for this type.

A few examples:

  • 0 for int and float
  • "" for string
  • nil for slice

func IsNotEmpty

func IsNotEmpty[T any](items []T) bool

Empty checks if the given slice is not empty

func IsNotNaN

func IsNotNaN[T comparable](value T) bool

func IsNotNil

func IsNotNil[T any](value *T) bool

func IsNotZero

func IsNotZero[T Number](n T) bool

IsNotZero checks if the given number is not zero

func IsZero

func IsZero[T Number](n T) bool

IsZero checks if the given number is zero

func LessThan

func LessThan[T constraints.Ordered](a T) func(T) bool

LessThan returns lambda that checks if an item is less than the given value.

func Max

func Max[T constraints.Ordered](a T, b T) T

Max returns maximal value

Example
package main

import (
	"fmt"

	"github.com/life4/genesis/lambdas"
)

func main() {
	res := lambdas.Max(10, 13)
	fmt.Println(res)
}
Output:

13

func Min

func Min[T constraints.Ordered](a T, b T) T

Min returns minimal value

Example
package main

import (
	"fmt"

	"github.com/life4/genesis/lambdas"
)

func main() {
	res := lambdas.Min(15, 13)
	fmt.Println(res)
}
Output:

13

func Must

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

Must wraps a function invicotaion and panic if it returned an error.

Example
package main

import (
	"fmt"

	"github.com/life4/genesis/lambdas"
	"github.com/life4/genesis/slices"
)

func main() {
	res := lambdas.Must(slices.Min([]int{42, 7, 13}))
	fmt.Println(res)
}
Output:

7

func Not

func Not[T constraints.Ordered](f func(T) bool) func(T) bool

Not negates the result of a lambda

func Safe

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

Safe wraps a function invicotaion and returns the empty value if it returned an error.

Example
package main

import (
	"fmt"

	"github.com/life4/genesis/lambdas"
	"github.com/life4/genesis/slices"
)

func main() {
	res := lambdas.Safe(slices.Min([]int{}))
	fmt.Println(res)
}
Output:

0

Types

type Number

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

Jump to

Keyboard shortcuts

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