result

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

package result is an experiment in monadic types. The main type is the Res[T], representing the result of some computation. It is useful to use Res in ion.Seq[Res[T]] sequences, since it can capture and propagate errors during I/O or other non-pure functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[T any](f func(e T)) func(Res[T])

Apply f func(T) -> func(Res[T]) Apply takes a function `f` that takes a value of type T and Apply returns a func that takes a value of type Res[T] and applies `f` to the value of type T contained in the result. If the Res[T] is an error result, the function `f` is not called on it.

func FMap

func FMap[T, U any](f func(e T) U) func(Res[T]) Res[U]

FMap f func(T) -> func(Res[T]) Res[U] FMap takes a function `f` that takes a value of type T and returns a value of type U. FMap returns a func that takes a value of type Res[T] and applies `f` to the value of type T contained in the result, wrapping the value of type U into a Result.

If the Res[T] is an error result, the function `f` is not called on it, and the Res[T] is converted to Res[U], its error.

func Handle

func Handle[T any](f func(e error)) func(Res[T]) Res[T]

Handle f func(error) -> func(Res[T]) Res[T] Handle takes a function designed to handle errors and returns a function that will apply that handler function to a Res[T], if the Res[T] is an error result.

func Map

func Map[T, U any](s ion.Seq[Res[T]], f func(T) U) ion.Seq[Res[U]]

Map s Seq[Res[T]] -> f func(T) U -> Seq[Res[U]] Map is analogous to ion.Map, only it maps a function from T to U over a Seq[Result[T]], returning Seq[Result[U]].

The resulting sequence contains f applied to the elements of type T of the Res[T]s where those Res[T]'s are not errors. Res[T] elements that are errors are converted from Res[T] to Res[U] retaining their errors.

Map(s, f) is equivalent to

ion.Map(s, FMap(f))

Types

type Res

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

Res contains the result of a computation. This can be a value of type T, or an error. Note: Experimental

func Err

func Err[T any](e error) Res[T]

Err returns a new Res[T] containing the error `e`.

func Ok

func Ok[T any](e T) Res[T]

OK returns a new Res[T] containing `e`.

func (*Res[T]) Get

func (r *Res[T]) Get() (T, error)

Get returns the value of type T, and any error held by the Res[T]. If ther error is not nil, the T is the zero value.

Jump to

Keyboard shortcuts

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