maybe

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MIT Imports: 1 Imported by: 0

README

Maybe

GitHub go.mod Go version of a Go module GoDoc reference example Go Report Card CircleCI

Simple zero dependency struct-based generic monad implementation in Go.

Installation

go get -u github.com/mono83/maybe

Usage

Instantiation

There are two main ways to create maybe.Maybe monadic container - it is either call to maybe.Nothing() or maybe.Just(value). First will create empty monad, while second one will produce not empty monad with given value.

Pay attention, that maybe.Just does not verify content of passed value, so constructions like maybe.Just[error](nil) can create not empty monads with nil value within. To avoid this behaviour use maybe.Nilable, that will perform nil check of given value.

In addition to raw instantiation, this library provide mapping constructor maybe.Map, able to convert Maybe from one type to another using mapping function

n := maybe.Nothing[string]()    // String type, but empty
i := maybe.Just(1)              // Int type
s := maybe.Map(i, strconv.Itoa) // String type
JSON

JSON bindings can be accessed using github.com/mono83/maybe/json package:

import "github.com/mono83/maybe/json"

type Request struct {
	ID       int
	ParentID json.Maybe[int]
}

Benchmarks
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: github.com/mono83/maybe
cpu: Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz
BenchmarkNothing-12             1000000000               0.2704 ns/op          0 B/op          0 allocs/op
BenchmarkJust-12                904382658                1.313 ns/op           0 B/op          0 allocs/op
BenchmarkPtr-12                 1000000000               1.051 ns/op           0 B/op          0 allocs/op
BenchmarkPtrNil-12              755984685                1.578 ns/op           0 B/op          0 allocs/op
BenchmarkNilableInt-12          72761029                16.37 ns/op            8 B/op          0 allocs/op
BenchmarkNilableNil-12          275986280                4.157 ns/op           0 B/op          0 allocs/op
PASS
ok      github.com/mono83/maybe 6.931s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Maybe

type Maybe[T any] struct {
	// contains filtered or unexported fields
}

Maybe is generic monadic container for value or it's absence

func Just

func Just[T any](value T) Maybe[T]

Just constructs new Maybe with given value. This constructor will create non-empty Maybe even for nil value, for protection use Nilable instead.

func Map

func Map[T any, R any](from Maybe[T], mapper func(T) R) Maybe[R]

Map constructs new Maybe container built using mapper function.

func Nilable

func Nilable[T any](value T) Maybe[T]

Nilable constructs new Maybe and performs emptiness check of given data. Will produce empty Maybe for following cases: - nil value - zero-length slice - zero-length map

func Nothing

func Nothing[T any]() Maybe[T]

Nothing constructs new Maybe with no content.

func Ptr

func Ptr[T any](value *T) Maybe[T]

Ptr constructs Maybe from pointer value. If nil given returns Nothing.

func (Maybe[T]) Filter

func (m Maybe[T]) Filter(predicate func(T) bool) Maybe[T]

Filter applies given predicate of container returning original value only if predicate resulted in true.

func (Maybe[T]) FilterNotNil

func (m Maybe[T]) FilterNotNil() Maybe[T]

FilterNotNil filters Maybe leaving only non-nil value.

func (Maybe[T]) Get

func (m Maybe[T]) Get() (T, bool)

Get returns value and is present flag.

func (Maybe[T]) IfPresent

func (m Maybe[T]) IfPresent(consume func(T)) Maybe[T]

IfPresent invokes given consumer callback only if container has value.

func (Maybe[T]) IfPresentOrElse

func (m Maybe[T]) IfPresentOrElse(consume func(T), els func()) Maybe[T]

IfPresent invokes given consumer callback if value present and plain func if container empty.

func (Maybe[T]) IsEmpty

func (m Maybe[T]) IsEmpty() bool

IsEmpty returns true if container has no data

func (Maybe[T]) IsPresent

func (m Maybe[T]) IsPresent() bool

IsPresent returns true if container has data

func (Maybe[T]) MapToAny

func (m Maybe[T]) MapToAny(mapper func(T) any) Maybe[any]

func (Maybe[T]) MapToFloat32

func (m Maybe[T]) MapToFloat32(mapper func(T) float32) Maybe[float32]

func (Maybe[T]) MapToFloat64

func (m Maybe[T]) MapToFloat64(mapper func(T) float64) Maybe[float64]

func (Maybe[T]) MapToInt

func (m Maybe[T]) MapToInt(mapper func(T) int) Maybe[int]

func (Maybe[T]) MapToInt16

func (m Maybe[T]) MapToInt16(mapper func(T) int16) Maybe[int16]

func (Maybe[T]) MapToInt32

func (m Maybe[T]) MapToInt32(mapper func(T) int32) Maybe[int32]

func (Maybe[T]) MapToInt64

func (m Maybe[T]) MapToInt64(mapper func(T) int64) Maybe[int64]

func (Maybe[T]) MapToString

func (m Maybe[T]) MapToString(mapper func(T) string) Maybe[string]

func (Maybe[T]) MapToUint

func (m Maybe[T]) MapToUint(mapper func(T) uint) Maybe[uint]

func (Maybe[T]) MapToUint16

func (m Maybe[T]) MapToUint16(mapper func(T) uint16) Maybe[uint16]

func (Maybe[T]) MapToUint32

func (m Maybe[T]) MapToUint32(mapper func(T) uint32) Maybe[uint32]

func (Maybe[T]) MapToUint64

func (m Maybe[T]) MapToUint64(mapper func(T) uint64) Maybe[uint64]

func (Maybe[T]) Or

func (m Maybe[T]) Or(supply func() Maybe[T]) Maybe[T]

Or produces Maybe: 1. If current not empty - return it 2. If current is empty - return produced by supply func 3. Nothing if current is empty and supply function is nil

func (Maybe[T]) OrElse

func (m Maybe[T]) OrElse(other T) T

OrElse returns value from Maybe if it presents, otherwise given value is returned.

func (Maybe[T]) OrElseGet

func (m Maybe[T]) OrElseGet(supply func() T) T

OrElseGet returns value from Maybe if it presents, otherwise value produced by supply function returnes.

func (Maybe[T]) Value

func (m Maybe[T]) Value() T

Value returns container value as-is. It is not recommended calling this method, use Maybe.Or, Maybe.OrElse, Maybe.OrElseGet or Maybe.Get instead.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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