maybe

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package maybe defines interfaces and structs for encapsulating a value (a Just) or an absent value (a Nothing).

Without Maybe, code must rely on a convention like nil or zero value to indicate that a value is not supplied. With a Maybe, the intention becomes more clear.

Two constructors are provided for creating a Maybe; Just which creates a Maybe encapsulating a value, and Nothing which creates a Maybe encapsulating the absence of a value. See those functions for more information.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallFunc

func CallFunc(functionResult Maybe[func()])

CallFunc is a helper function that will call a function with no return value that is encapsulated in a Maybe. This is useful when deferring a call to a function that was returned in a Maybe.

func IsJust

func IsJust[T any](value Maybe[T]) bool

IsJust returns true if the given maybe.Maybe is a maybe.Just. This function exists so that it can be used in a result.Map* call.

Types

type FlatMapper

type FlatMapper[T, R any] func(T) Maybe[R]

FlatMapper defines a function with one argument that maps from the argument's type to a Maybe of that type.

type Getter

type Getter[T any] func() T

Getter defines a function that gets the default value for a Maybe that encapsulates an absent value.

type MapperNoError

type MapperNoError[T, R any] func(
	T,
) R

MapperNoError defines a function with one argument of any type that returns a single value.

type Maybe

type Maybe[T any] interface {
	// IsJust returns true if the Maybe encapsulates a value and false if the
	// Maybe encapsulates nothing.
	IsJust() bool
	// MustGet returns the encapsulated value if the Maybe encapsulates a value
	// and panics if the Maybe encapsulates nothing.
	MustGet() T
	// OrElse returns the encapsulated value if the Maybe encapsulates a value
	// and returns the given default if the Maybe encapsulates nothing.
	OrElse(d T) T
	// OrElseGet returns the encapsulated value if the Maybe encapsulates a value
	// and returns the result of calling the given Getter function if the Maybe
	// encapsulates nothing.
	OrElseGet(Getter[T]) T
}

Maybe encapsulates an optional value that may or may not exist.

func FlatMap

func FlatMap[T, R any](f FlatMapper[T, R], m Maybe[T]) Maybe[R]

FlatMap applies the given FlatMapper to the Maybe. If the Maybe encapsulates a value then that value is passed to the Mapper and the Mapper's return value is returned. If the Maybe encapsulates an absent value then Maybe encapsulating an absent value of the return type of the Mapper is returned.

func Just

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

Just returns a Maybe encapsulating a value.

func Map

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

Map applies the given MapperNoError to the Maybe. If the Maybe encapsulates a value then that value is passed to the Mapper and a Maybe of the return type is returned. If the Maybe encapsulates nothing then the nothing is propagated to a Maybe of the return type.

func Nothing

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

Nothing returns a Maybe encapsulating an absent value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳