Skip to content

Instantly share code, notes, and snippets.

@shovon
Last active July 26, 2019 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shovon/d38556e40f9d29d773451ad8c7579bdc to your computer and use it in GitHub Desktop.
Save shovon/d38556e40f9d29d773451ad8c7579bdc to your computer and use it in GitHub Desktop.
Some initial work on monads in Golang
package monad
type ThenCallback func(interface{}) (Thenable, error)
type Thenable interface {
Then(ThenCallback) Thenable
Unwrap() interface{}
}
type Error struct {
err error
}
func (e Error) Content() error {
return e.err
}
func (e Error) Then(f ThenCallback) Thenable {
return e
}
func (e Error) Unwrap() error {
return nil
}
type Unit struct {
value interface{}
}
func (t Unit) Then(f ThenCallback) Thenable {
value, err := f(t.Unwrap())
if err != nil {
return Error{err}
}
return Unit{value.Unwrap())
}
func (t Unit) Unwrap() interface{} {
return t.value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment