either

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2022 License: MIT Imports: 1 Imported by: 0

README

go-either

An Either type for golang with generics

e := either.NewLeft[int, string](1) // either.Type[int, string]

l, r, isLeft := e.Unwrap()
if isLeft {  // true
  print(l)   // l is 1
} else {
  print(r)
}

e.EitherDo(func(l int) {
  print(l)
}, func(r string) {
  print(r)
})

either.MapLeft(e, func(left int) *int {
  return &left
}) // either.Type[*int, string]

// either.MapRight
// either.Map

and the Result type, based on either.Type

r := result.New(123) // result.Type[int]
r.IsError() // false

if w, err := r.Unwrap(); err != nil {
  print(w) // 123
}

result.Map(r, func(t int) string {
  return "123"
}) // result.Type[string]

r.MapError(func(err error) error {
  return fmt.Errorf("faile: %w", err)
}) // equals r, as MapError will only execute when r is error result

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Map

func Map[Left, Right, R any](s Type[Left, Right], leftCase func(Left) R, rightCase func(Right) R) R

Types

type Type

type Type[T, U any] struct {
	// contains filtered or unexported fields
}

func MapLeft

func MapLeft[Left, Right, LeftNew any](e Type[Left, Right], f func(Left) LeftNew) Type[LeftNew, Right]

func MapRight

func MapRight[Left, Right, RightNew any](e Type[Left, Right], f func(Right) RightNew) Type[Left, RightNew]

func NewLeft

func NewLeft[T, U any](v T) Type[T, U]

func NewRight

func NewRight[T, U any](v U) Type[T, U]

func (Type[Left, Right]) EitherDo

func (s Type[Left, Right]) EitherDo(leftCase func(Left), rightCase func(Right))

func (Type[T, U]) Unwrap

func (s Type[T, U]) Unwrap() (left T, right U, isLeft bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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