Last active
July 26, 2019 03:15
-
-
Save shovon/d38556e40f9d29d773451ad8c7579bdc to your computer and use it in GitHub Desktop.
Some initial work on monads in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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