option

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2023 License: MIT Imports: 3 Imported by: 0

README

Go Reference

Functional Options

Optional types are common in a number of programming languages today. The API for this Option type is based on the standard library for F# although it is not completely the same.

Get it

go get -u github.com/flowonyx/functional/option

Use it

import "github.com/flowonyx/functional/option"

Types

Optional interface

There may be a reason to use a different type for optional values but still want to use these functions to work with it. The interface Optional is pretty simple:

type Optional[T any] interface {
  IsSome() bool
  IsNone() bool
  Value() T
}

There is a smaller interface the leaves out Value, and it is used in any functions that only need to test for IsSome or IsNone.

type OptionalCheckOnly interface {
  IsSome() bool
  IsNone() bool
}

Option type

The Option type implements the Optional interface and has some additional functions associated with it.

Optional Functions

  • HandleOption accepts a input that is an Optional value and two functions that return errors.
    • whenSome is the function to use when o.IsSome() is true.
    • whenNone is the function to use when o.IsNone() is true.
    • The error returned by the function that is used will be returned from HandleOption.
  • HandleOptionIgnoreNone accepts an Optional value and one function that returns an error.
    • whenSome is the function to use when o.IsSome() is true.
    • The error returned by whenSome will be returned from HandleOption or nil will be returned when o.IsNone().
  • DefaultValue tests whether o.IsNone() and returns the given default value if true.
    • If o.IsSome(), o.Value() is returned.
  • DefaultWith tests whether o.IsNone() and returns the result of a function if true.
    • If o.IsSome(), o.Value() is returned.
  • Contains tests whether the value in the Optional is equal to the test value.
  • Count returns 0 if o.IsNone() and 1 if o.IsSome().
  • Exists tests is the value in the Optional o matches the predicate.
    • If o.IsNone(), it will return false.
  • Fold applies the folder function to the value in an Optional.
    • If o.IsNone(), it will return the initial state.
    • If o.IsSome(), it will return the result of the function.
  • FoldBack applies the folder function to the value in an Optional. If o.IsNone(), it will return the initial state.
    • If o.IsSome(), it will return the result of the function.
    • It is the same as Fold, but with the parameters swapped.
  • ForAll returns true if either o.IsNone() or the predicate function returns true when applied to the value of the Optional.
    • It returns false only if the predicate function returns false.
  • Get retrieves the value in an Optional and panics if o.IsNone().
  • IsNone checks if an Optional is None.
  • IsSome checks if Optional is Some.
  • Iter applies an action function to the value of an Optional. If o.IsNone(), this does nothing.
  • OrElse returns a given Optional if o.IsNone(). Otherwise, it returns o.
  • OrElseWith returns the result of a function if o.IsNone(). Otherwise it returns o.
  • ToSlice creates a single item slice from the value in an Optional.
    • If o.IsNone(), it returns an empty slice.
  • ToNullable returns nil if o.IsNone().
    • Otherwise, it returns a pointer the value of the Optional.

Option Functions

These functions need to have access to the actual type, not just an interface.

  • Some creates an Option with a value.
  • None creates an Option with no value.
  • IsSome tests if the Option contains a value.
  • IsNone tests whether the Option does not contain a value.
  • Value returns the value in the Option. If the Option is None, it returns the zero value for the type.
  • Bind applies a function to an Option o if o.IsSome() and otherwise returns None.
  • Filter returns an Option if the value in it matches the predicate. Otherwise, it returns None.
  • Flatten takes a nested Option and returns the inner Option.
  • Map applies a function to the value of an Option and returns the result as an Option. If the given Option is None, it returns None.
  • Map2 applies a function to the values in two Options as the first and second parameters and returns the result as an Option. If either Option is None, it returns None.
  • Map3 applies a function to the values in three Options as the first, second, and third parameters and returns the result as an Option. If any of the Options are None, it returns None.
  • OfNullable returns None if the supplied pointer is nil. Otherwise it returns Some of the value (after dereferencing the pointer).
  • Lift converts a function that returns a value and an error to a function that returns an Option.
  • Lift1 converts a function that accepts a single input and returns a value and an error to a function that accepts a single input and returns an Option.
  • Lift2 converts a function that accepts two inputs and returns a value and an error to a function that accepts two inputs and returns an Option.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable, TOptional Optional[T]](value T, o TOptional) bool

Contains tests whether the value in the Optional value o is equal to value.

func Count

func Count(o OptionalCheckOnly) int

Count returns 0 if o.IsNone() and 1 if o.IsSome().

func DefaultValue

func DefaultValue[T any, TOptional Optional[T]](value T, o TOptional) T

DefaultValue tests whether o.IsNone() and returns value if true. If o.IsSome(), o.Value() is returned.

func DefaultWith

func DefaultWith[T any, TOptional Optional[T]](defThunk func() T, o TOptional) T

DefaultWith tests whether o.IsNone() and returns the result of defThunk if true. If o.IsSome(), o.Value() is returned.

func Exists

func Exists[T any, TOptional Optional[T]](predicate func(T) bool, o TOptional) bool

Exists tests is the value in the Optional o matches the predicate. If o.IsNone(), it will return false.

func Fold

func Fold[T, State any, TOptional Optional[T]](folder func(State, T) State, s State, o TOptional) State

Fold applies the folder function to the value in o. If o is None, it will return s. If o is Some, it will return the result of the function, with s being passed as the first parameter and the value of o being the second parameter.

func FoldBack

func FoldBack[T, State any, TOptional Optional[T]](folder func(T, State) State, o TOptional, s State) State

FoldBack applies the folder function to the value in o. If o is None, it will return s. If o is Some, it will return the result of the function, with the value of o being passed as the first parameter and s being the second parameter. It is the same as Fold, but with the parameters swapped.

func ForAll

func ForAll[T any, TOptional Optional[T]](predicate func(T) bool, o TOptional) bool

ForAll returns true if either o is None or the predicate returns true when applied to the value of o. It returns false only if the predicate returns false.

func Get

func Get[T any, TOptional Optional[T]](o TOptional) T

Get retrieves the value in o and panics if o is None.

func HandleOption

func HandleOption[T any, TOptional Optional[T], F func(T) error, FN func() error](o TOptional, whenSome F, whenNone FN) error

HandleOption accepts an Optional value and two functions that return errors. whenSome is the function to use when o.IsSome() is true. whenNone is the function to use when o.IsNone() is true. The error returned by the function that is used will be returned from HandleOption.

Example
package main

import (
	"fmt"

	"github.com/flowonyx/functional/option"
)

func main() {
	input := option.Some(1)
	err := option.HandleOption(input, func(i int) error {
		fmt.Printf("%d", i)
		return nil
	}, func() error {
		fmt.Println("None")
		return nil
	})
	if err != nil {
		panic(err)
	}
}
Output:

1

func HandleOptionIgnoreNone

func HandleOptionIgnoreNone[T any, TOptional Optional[T], F func(T) error](o TOptional, whenSome F) error

HandleOptionIgnoreNone accepts an Optional value and one function that returns an error. whenSome is the function to use when o.IsSome() is true. The error returned by whenSome will be returned from HandleOptionIgnoreNone or nil will be returned when o.IsNone().

Example
package main

import (
	"fmt"

	"github.com/flowonyx/functional/option"
)

func main() {
	input := option.None[int]()
	err := option.HandleOptionIgnoreNone(input, func(i int) error {
		fmt.Printf("%d", i)
		return nil
	})
	if err != nil {
		panic(err)
	}
}
Output:

func IsNone

func IsNone[T any, TOptional Optional[T]](o TOptional) bool

IsNone checks if o is None.

func IsSome

func IsSome[T any, TOptional Optional[T]](o TOptional) bool

IsSome checks if o is Some.

func Iter

func Iter[T any, TOptional Optional[T]](action func(T), o TOptional)

Iter applies the action to the value of o. If o is None, this does nothing.

func Lift

func Lift[T any](f func() (T, error)) func() Option[T]

Lift converts the function f that returns a value and an error to a function that returns an Option.

func Lift1

func Lift1[TInput, T any](f func(TInput) (T, error)) func(TInput) Option[T]

Lift1 converts the function f that accepts a single input and returns a value and an error to a function that accepts a single input and returns an Option.

func Lift2

func Lift2[TInput1, TInput2, T any](f func(TInput1, TInput2) (T, error)) func(TInput1, TInput2) Option[T]

Lift2 converts the function f that accepts two inputs and returns a value and an error to a function that accepts two inputs and returns an Option.

func OrElse

func OrElse[T any, TOptional Optional[T]](ifNone TOptional, o TOptional) TOptional

OrElse returns ifNone if o is None. Otherwise, it returns o.

func OrElseWith

func OrElseWith[T any, TOptional Optional[T]](ifNoneThunk func() TOptional, o TOptional) TOptional

OrElseWith returns the return value of ifNoneThunk if o is None. Otherwise it returns o.

func ToNullable

func ToNullable[T any, TOptional Optional[T]](o TOptional) *T

ToNullable returns nil if o is None. Otherwise, it returns a pointer the value of o.

func ToSlice

func ToSlice[T any, TOptional Optional[T]](o TOptional) []T

ToSlice creates a single item slice from the value in o. If o is None, it returns an empty slice.

Types

type Option

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

Option is a type that allows optional values. It is similar to a Sum type and can either be Some(value) or None.

func Bind

func Bind[T, R any](f func(T) Option[R], input Option[T]) Option[R]

Bind applies f to input if input.IsSome() and otherwise returns None.

Example
package main

import (
	"fmt"

	"github.com/flowonyx/functional/option"
)

func main() {
	input := option.Some(2)
	r := option.Bind(func(i int) option.Option[int] { return option.Some(i * 2) }, input)
	fmt.Println(r)
	// Some(4)
}
Output:

func Filter

func Filter[T any](predicate func(T) bool, o Option[T]) Option[T]

Filter returns o if the value in o matches the predicate. Otherwise, it returns None.

func Flatten

func Flatten[T any](oo Option[Option[T]]) Option[T]

Flatten takes a nested option and returns the inner option.

func Map

func Map[T, R any](f func(T) R, o Option[T]) Option[R]

Map applies f to the value of o and returns the result as an Option. If o is None, it returns None.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/flowonyx/functional"
	"github.com/flowonyx/functional/list"
	"github.com/flowonyx/functional/option"
)

func main() {
	f := func(i int) string { return "i:" + strconv.Itoa(i) }
	input := []option.Option[int]{option.None[int](), option.Some(1)}
	r := list.Map(functional.Curry2To1(option.Map[int, string], f), input)
	fmt.Println(r)
}
Output:

[None Some("i:1")]

func Map2

func Map2[T1, T2, R any](f func(T1, T2) R, o1 Option[T1], o2 Option[T2]) Option[R]

Map2 applies f to the values in both o1 and o2 as the first and second parameters and returns the result as an Option. If either option is None, it returns None.

func Map3

func Map3[T1, T2, T3, R any](f func(T1, T2, T3) R, o1 Option[T1], o2 Option[T2], o3 Option[T3]) Option[R]

Map3 applies f to the values in o1, o2, and o3 as the first, second, and third parameters and returns the result as an Option. If any of the options are None, it returns None.

func None

func None[T any]() Option[T]

None creates an option with no value.

func OfNullable

func OfNullable[T any](value *T) Option[T]

OfNullable returns None if value is nil. Otherwise it returns Some of the value (after dereferencing the pointer).

func Some

func Some[T any](value T) Option[T]

Some creates an option with a value.

func (Option[T]) IsNone

func (o Option[T]) IsNone() bool

IsNone tests whether the option does not contain a value.

func (Option[T]) IsSome

func (o Option[T]) IsSome() bool

IsSome tests if the option contains a value.

func (Option[T]) String

func (o Option[T]) String() string

func (Option[T]) Value

func (o Option[T]) Value() T

Value returns the value in the option. If the option is None, it returns the zero value for the type.

type Optional

type Optional[T any] interface {
	IsSome() bool
	IsNone() bool
	Value() T
}

Optional is an interface for Option-like types.

type OptionalCheckOnly

type OptionalCheckOnly interface {
	IsSome() bool
	IsNone() bool
}

OptionalCheckOnly is an interface for Option-like types when Value is not required.

Jump to

Keyboard shortcuts

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