Documentation ¶
Overview ¶
Package result implements a R{value, error} "sum type" that has a value only when error is nil.
Note that in many cases, it is more idiomatic for a function to return a naked (value, error). Use WrapFunc to convert such a function to return a R result type.
Index ¶
- func Applicator[X any, Y any](f R[func(x X) Y]) func(x X) R[Y]
- func Compose[X any, Y any, Z any](xy func(R[X]) R[Y], yz func(R[Y]) R[Z]) func(R[X]) R[Z]
- func FlatMap[X any, Y any](f func(x X) R[Y]) func(x2 R[X]) R[Y]
- func Lift[X any, Y any](f func(x X) Y) func(x X) R[Y]
- func Map[X any, Y any](f func(x X) Y) func(x2 R[X]) R[Y]
- func UnwrapFunc[A any, T any](f func(A) R[T]) func(A) (T, error)
- func WrapFunc[X any, Y any](f func(x X) (Y, error)) func(x X) R[Y]
- type R
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Applicator ¶ added in v2.2.0
Applicator turns function "R[f]: X => Y" into "f: X => R[Y]".
func Compose ¶ added in v2.2.0
Compose takes two functions of the form "xy: R[X] => R[Y]" and "yz: R[Y] => R[Z]" and returns a function "xz(R[X]) => R[Z]".
func Lift ¶
Lift converts a function of the form "f: X => Y" to the form "f: X => R[Y]" where R[Y] == Some(y).
func UnwrapFunc ¶
UnwrapFunc converts a function of the form "f(a) => R[T]" to the form "f(a) => (T, error)".
Types ¶
type R ¶ added in v2.2.0
R is a (value, error) "sum type" that has a value only when error is nil.
Example ¶
package main import ( "fmt" "io" "os" "strings" "github.com/tawesoft/golib/v2/fun/result" "github.com/tawesoft/golib/v2/fun/slices" ) func main() { resultOpen := result.WrapFunc(os.Open) toReader := result.Map(func(x *os.File) io.Reader { return x }) closer := func(f *os.File) error { fmt.Printf("Closing %s\n", f.Name()) return f.Close() } paths := []string{ "testdata/example1.txt", "testdata/example2.txt", "testdata/example3.txt", } handles := slices.Map(resultOpen, paths) defer slices.Map(result.Map(closer), handles) readers := slices.Map(toReader, handles) resultRead := result.FlatMap(result.WrapFunc(io.ReadAll)) toString := result.Map(func(x []byte) string { return string(x) }) resultReadString := result.Compose(resultRead, toString) contents := slices.Map(resultReadString, readers) for _, x := range contents { if !x.Success() { err := x.Error if os.IsNotExist(err) { // hack to ensure error has the same text on every OS err = fmt.Errorf("no such file or directory") } fmt.Printf("Could not read from file: %v\n", err) continue } fmt.Println(strings.TrimSpace(x.Value)) } }
Output: This is the first file! Could not read from file: no such file or directory This is the third file! Closing testdata/example1.txt Closing testdata/example3.txt
func New ¶
New returns a R. It is syntax sugar for R{value, error}. If error is a known constant, use Some or Error instead.
func (R[V]) Else ¶ added in v2.2.0
func (r R[V]) Else(v V) V
Else returns R.value if not an error, otherwise returns the provided argument instead.
func (R[V]) JoinError ¶ added in v2.7.0
JoinError returns a new Error based on an existing R. If the existing R is not an error, returns Error(err). Otherwise, returns Error(errors.Join(existingError, err)).
func (R[V]) Must ¶ added in v2.2.0
func (r R[V]) Must() V
Must returns a R's value. If the R is an error, panics.
func (R[V]) MustError ¶ added in v2.2.0
MustError returns a R's error. Panics if the R is not an error.