maybe

package
v0.0.5-0...-752e31d Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2022 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Maybe

type Maybe[T any] interface {
	Unwrap() *T
	Map(func(T) T) Maybe[T]
	Apply(func(T)) Maybe[T]
	Bind(func(T) Maybe[T]) Maybe[T]
	IsSome() bool
	IsNil() bool
	OrElse(func() T) *T
	Or(T) *T
	OrNil() *T
}

Maybe is a monadic pattern allowing for data manipulation while abstracting whether the value actually exists or is nil. For example, if we fetch data from an external API that could be nil, we can still perform manipulation on it while disregarding its actual state. The Maybe struct will take care of managing the value itself. This is similar to the Maybe interface in Elm or Haskell or Optional in Java. This is helpful for CRUD operations by simplifying the code and allowing for seamless manipulation of nullable data.

func Just

func Just[T nillable](val T) Maybe[T]

Just returns a new Maybe based on a value that we know is not nil.

func Map

func Map[T2, T any](m Maybe[T], f func(T) T2) Maybe[T2]

func None

func None[T nillable]() Maybe[T]

None returns a new Maybe with an empty value we know is nil.

func Of

func Of[T nillable](val *T) Maybe[T]

Of returns a new Maybe based on a value that may or may not be nil.

Jump to

Keyboard shortcuts

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