either

package
v0.0.0-...-c2e7105 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2017 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Example (ChainAndThen)
package main

import (
	"fmt"

	"github.com/asteris-llc/gofpher/either"
	"github.com/asteris-llc/gofpher/monad"
)

func toNum(i interface{}) either.EitherM {
	if num, ok := i.(int); ok {
		return either.RightM(num)
	}
	return either.LeftM(i)
}

func addOne(src interface{}) monad.Monad {
	num := src.(int)
	if num == 7 {
		return toNum("seven")
	}
	return toNum(num + 1)
}

func main() {
	plusOne := func(i interface{}) interface{} { return 1 + i.(int) }
	a := either.RightM(1).AndThen(addOne).AndThen(addOne).AndThen(addOne)
	fmt.Println(a)
	b := monad.FMap(plusOne, a)
	fmt.Println(b)
}
Output:

Right (4)
Right (5)
Example (Either)
package main

import (
	"fmt"

	"github.com/asteris-llc/gofpher/either"
	"github.com/asteris-llc/gofpher/monad"
)

func toNum(i interface{}) either.EitherM {
	if num, ok := i.(int); ok {
		return either.RightM(num)
	}
	return either.LeftM(i)
}

func addOne(src interface{}) monad.Monad {
	num := src.(int)
	if num == 7 {
		return toNum("seven")
	}
	return toNum(num + 1)
}

func show(a, b either.EitherM) {
	fmt.Println(a)
	fmt.Println(b)
}

func main() {
	a := either.RightM(1)
	b := either.LeftM("foo")
	show(a, b)
	a1 := a.AndThen(addOne)
	b1 := b.AndThen(addOne)
	show(a1.(either.EitherM), b1.(either.EitherM))
}
Output:

Right (1)
Left (foo)
Right (2)
Left (foo)
Example (EitherFMap)
package main

import (
	"fmt"

	"github.com/asteris-llc/gofpher/either"
	"github.com/asteris-llc/gofpher/monad"
)

func main() {
	plusOne := func(i interface{}) interface{} { return 1 + i.(int) }
	a := either.ReturnM(1)
	b := monad.FMap(plusOne, a)
	fmt.Println(b)
	b = monad.FMap(plusOne, b)
	fmt.Println(b)
	b = monad.FMap(plusOne, b)
	fmt.Println(b)
}
Output:

Right (2)
Right (3)
Right (4)
Example (Join)
package main

import (
	"fmt"

	"github.com/asteris-llc/gofpher/either"
	"github.com/asteris-llc/gofpher/monad"
)

func main() {
	a := either.RightM(either.RightM("foo"))
	fmt.Println(a)
	fmt.Println(monad.Join(a))
	a = either.LeftM(either.LeftM("foo"))
	fmt.Println(a)
	fmt.Println(monad.Join(a))
}
Output:

Right (Right (foo))
Right (foo)
Left (Left (foo))
Left (Left (foo))

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromLeft

func FromLeft(e Either) (interface{}, bool)

FromLeft attempts to coerce the either into a left type value

func FromRight

func FromRight(e Either) (interface{}, bool)

FromRight attempts to coerce the either into a right type value

Types

type AndThenFunc

type AndThenFunc func(interface{}) Either

AndThenFunc is a type alias for a function that takes a param and returns Either

type Either

type Either interface {
	AndThen(AndThenFunc) Either
}

Either represents the Either monad. LeftType.AndThen returns identity, RightType.AndThen returns the result of applying the function to the element.

func Left

func Left(i interface{}) Either

Left creates a new left type value

func Right(i interface{}) Either

Right creates a new right type value

type EitherM

type EitherM struct {
	Either
}

EitherM provides a full monadic wrapper around Either with Return so that it implements monad.Monad. An either may be a LeftType or a RightType. By convention LeftType represents some terminal or error condition, and RightType represents some intermediate or terminal phase of computation. It is common to use Either in a way similar ot multi-return functions, e.g. Either error Type.

func LeftM

func LeftM(i interface{}) EitherM

LeftM generates a left instance of EitherM

func ReturnM

func ReturnM(i interface{}) EitherM

ReturnM returns and EitherM

func RightM

func RightM(i interface{}) EitherM

RightM generates a left instance of EitherM

func (EitherM) AndThen

func (m EitherM) AndThen(f func(interface{}) monad.Monad) monad.Monad

AndThen wraps the underlying Either's AndThen

func (EitherM) FromEither

func (m EitherM) FromEither() (interface{}, bool)

FromEither returns the value and true if it was a right-hand value

func (EitherM) FromLeft

func (m EitherM) FromLeft() interface{}

FromLeft gets the left-hand value

func (EitherM) FromRight

func (m EitherM) FromRight() interface{}

FromRight gets the right-hand value

func (EitherM) IsLeft

func (m EitherM) IsLeft() bool

IsLeft returns true if the value is Left

func (EitherM) IsRight

func (m EitherM) IsRight() bool

IsRight returns true if the value is Right

func (EitherM) LogAndThen

func (m EitherM) LogAndThen(f func(interface{}) monad.Monad, logger func(interface{})) monad.Monad

LogAndThen wraps the underlying Either's AndThen and provides logging

func (EitherM) Return

func (m EitherM) Return(i interface{}) monad.Monad

Return creates a Right value

func (EitherM) String

func (m EitherM) String() string

Show shows the string

type LeftType

type LeftType struct{ Val interface{} }

LeftType represents the left-hand side of an Either

func (LeftType) AndThen

func (l LeftType) AndThen(AndThenFunc) Either

AndThen on LeftType returns the type without function application

type RightType

type RightType struct{ Val interface{} }

RightType represents the right-hand side of an Either

func (RightType) AndThen

func (r RightType) AndThen(f AndThenFunc) Either

AndThen on the RightType returns the result of function application

Jump to

Keyboard shortcuts

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