fn

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 1 Imported by: 0

README

fn

Go Reference Go Report Card Coverage Status

(yet another) functional primitives for Go -> Option with sql.Scanner / driver.Valuer functionality, JSON-serializable Result, composition, transactions primitive and moooore...

Install

go get github.com/pyrorhythm/fn

License

MIT

Documentation

Overview

Package fn provides functional programming primitives and different helpers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cast added in v0.3.1

func Cast[T any](v any) T

func Else added in v0.4.0

func Else[T any, M Container[T]](m M, fallback T) T

Else returns the value if valid, otherwise returns fallback.

func Fold added in v0.4.0

func Fold[T, U any, M Container[T]](m M, onNil func() U, onVal func(T) U) U

Fold pattern matches over a container, calling onNil if invalid, onVal if valid.

func If

func If[T any](cond bool, then T, or T) T

If is a generic ternary operator.

func Is added in v0.3.1

func Is[T any](v any) bool

func Must added in v0.4.0

func Must[T any, M Container[T]](m M) T

Must returns the value if valid, otherwise panics.

func Or

func Or[T any](p *T, v T) T

Or returns first pointer as a value if pointer is not nil.

Else returns fallback T value, which was passed as a second argument.

func OrZero

func OrZero[T any](p *T) T

OrZero validates pointer and returns pointer as a value if pointer is not nil.

Else returns zero T value.

func Valid

func Valid[T comparable](v T) bool

Valid returns whether T is non-zero, where T implements [comparable]

func ValidReflect

func ValidReflect[T any](v T) bool

ValidReflect - valid through type-casting and [reflectValue]

NOT recommended if not implementing interfaces below, as reflection in Go may be from 25 to 50 times slower than stdlib.

If you really want to, you can check casted interfaces below, and implement one of them to avoid using reflection.

func Z added in v0.3.1

func Z[T any]() T

Z ...wait... OH! It's Zero Value!

Types

type Container added in v0.4.0

type Container[T any] interface {
	Val() T
	Valid() bool
}

Container provides value extraction from monadic types.

Directories

Path Synopsis
Package chain provides helpers for chaining fallible functions in a res.Of pipeline.
Package chain provides helpers for chaining fallible functions in a res.Of pipeline.
Package opt implements the Of monad for Go.
Package opt implements the Of monad for Go.
Package res implements the Of monad for Go.
Package res implements the Of monad for Go.

Jump to

Keyboard shortcuts

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