result

package
v2.16.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 2 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Applicator added in v2.2.0

func Applicator[X any, Y any](
	f R[func(x X) Y],
) func(x X) R[Y]

Applicator turns function "R[f]: X => Y" into "f: X => R[Y]".

func Compose added in v2.2.0

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]

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 FlatMap added in v2.2.0

func FlatMap[X any, Y any](
	f func(x X) R[Y],
) func(x2 R[X]) R[Y]

FlatMap turns function "f: X => R[Y]" into "f: R[X] => R[Y]".

func Lift

func Lift[X any, Y any](
	f func(x X) Y,
) func(x X) R[Y]

Lift converts a function of the form "f: X => Y" to the form "f: X => R[Y]" where R[Y] == Some(y).

func Map

func Map[X any, Y any](
	f func(x X) Y,
) func(x2 R[X]) R[Y]

Map turns function "f: X => Y" into "f: R(X) => R[Y]".

func UnwrapFunc

func UnwrapFunc[A any, T any](
	f func(A) R[T],
) func(A) (T, error)

UnwrapFunc converts a function of the form "f(a) => R[T]" to the form "f(a) => (T, error)".

func WrapFunc

func WrapFunc[X any, Y any](
	f func(x X) (Y, error),
) func(x X) R[Y]

WrapFunc converts a function of the form "f(a) => (a, error)" to the form "f(a) => R[Y]".

Types

type R added in v2.2.0

type R[V any] struct {
	Value V
	Error error
}

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 Error

func Error[V any](err error) R[V]

Error returns a R that is an error.

func New

func New[V any](value V, err error) R[V]

New returns a R. It is syntax sugar for R{value, error}. If error is a known constant, use Some or Error instead.

func Some

func Some[V any](value V) R[V]

Some returns a R that contains a value and is not an error.

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

func (r R[V]) JoinError(err error) R[V]

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

func (r R[V]) MustError() error

MustError returns a R's error. Panics if the R is not an error.

func (R[V]) Success added in v2.2.0

func (r R[V]) Success() bool

Success returns true if the R is not an error.

func (R[V]) Unpack added in v2.2.0

func (r R[V]) Unpack() (V, error)

Unpack returns a plain (value, error) tuple from a R.

Jump to

Keyboard shortcuts

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