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 ¶
- func FromLeft(e Either) (interface{}, bool)
- func FromRight(e Either) (interface{}, bool)
- type AndThenFunc
- type Either
- type EitherM
- func (m EitherM) AndThen(f func(interface{}) monad.Monad) monad.Monad
- func (m EitherM) FromEither() (interface{}, bool)
- func (m EitherM) FromLeft() interface{}
- func (m EitherM) FromRight() interface{}
- func (m EitherM) IsLeft() bool
- func (m EitherM) IsRight() bool
- func (m EitherM) LogAndThen(f func(interface{}) monad.Monad, logger func(interface{})) monad.Monad
- func (m EitherM) Return(i interface{}) monad.Monad
- func (m EitherM) String() string
- type LeftType
- type RightType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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.
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 (EitherM) FromEither ¶
FromEither returns the value and true if it was a right-hand value
func (EitherM) FromRight ¶
func (m EitherM) FromRight() interface{}
FromRight gets the right-hand value
func (EitherM) LogAndThen ¶
LogAndThen wraps the underlying Either's AndThen and provides logging