either

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[A, B, C any](fL func(a A) C, fR func(b B) C, v Either[A, B]) C

Apply returns the default value `dflt` if `v` is Nothing. Otherwise it returns the result of calling `f` on `v`.

func Lefts

func Lefts[A, B any](es list.List[Either[A, B]]) (vs list.List[A])

Types

type Class

type Class[A, B any] interface {
	NewLeft(A) Left[A, B]
	NewRight(B) Right[A, B]
}

type Either

type Either[A, B any] interface {
	// contains filtered or unexported methods
}

Either is the sum type for Either.

Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/either"
	"github.com/calebcase/base/data/list"
)

func main() {
	l := either.Left[int, string]{1}
	fmt.Println(l.Value)

	r := either.Right[int, string]{"one"}
	fmt.Println(r.Value)

	{
		es := list.List[either.Either[string, int]]{
			either.Left[string, int]{"foo"},
			either.Right[string, int]{3},
			either.Left[string, int]{"bar"},
			either.Right[string, int]{7},
			either.Left[string, int]{"baz"},
		}
		fmt.Println(either.Lefts(es))
	}

	{
		t := either.NewType[string, int]()

		es := list.List[either.Either[string, int]]{
			t.NewLeft("foo"),
			t.NewRight(3),
			t.NewLeft("bar"),
			t.NewRight(7),
			t.NewLeft("baz"),
		}
		fmt.Println(either.Lefts(es))
	}

}
Output:

1
one
[foo bar baz]
[foo bar baz]

type Left

type Left[A, B any] struct {
	Value A
}

Left contains left value A.

func (Left[A, B]) DEmpty added in v0.0.6

func (l Left[A, B]) DEmpty() bool

func (Left[A, B]) DRest

func (l Left[A, B]) DRest() data.Data[A]

func (Left[A, B]) DValue

func (l Left[A, B]) DValue() A
type Right[A, B any] struct {
	Value B
}

Right contains the right value.

func (Right[A, B]) DEmpty added in v0.0.6

func (r Right[A, B]) DEmpty() bool

func (Right[A, B]) DRest

func (r Right[A, B]) DRest() data.Data[B]

func (Right[A, B]) DValue

func (r Right[A, B]) DValue() B

type Type

type Type[A, B any] struct{}

func NewType

func NewType[A, B any]() Type[A, B]

func (Type[A, B]) NewLeft

func (t Type[A, B]) NewLeft(v A) Left[A, B]

func (Type[A, B]) NewRight

func (t Type[A, B]) NewRight(v B) Right[A, B]

Jump to

Keyboard shortcuts

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