Functional Patterns in Golang
GOMAD (Early stage)
This package is still in an early stage of development. Feel free to open a PR and contribute or just open an issue to help me priorities features.
Following is a list of future modules coming up in no specific order:
- Maybe
- Either
- Result
- List
- Pipe
- HKT
- Monad
- IO
- Task
- Reader
Prerequisites
All these modules use the newly added features of Golang v1.18 (Still in beta as of today) notably type and function generics.
Install
go get github.com/medmouine/gomad/<Desired module>
i.e
go get github.com/medmouine/gomad/maybe
Modules
Maybe
Maybe
is a monadic pattern allowing for data manipulation while abstracting whether or not the value actually exists or is nil
. For example, if we fetch data from an external API that could be nil
, we can still perform manipulation on it while disregarding its actual state. The Maybe
struct will take care of managing the value itself. This is similar to the Maybe interface in Elm or Haskell or Optional in Java.
You can use the functions Just
, None
or Nillable
to instanciate a Maybe
struct. The type parameter will be determined by the passed argument or by specifying it. For example:
maybeNilInteger := maybe.Nillable[int](nil)
nilInteger := maybe.None[int]()
someInteger := maybe.Just[int](1)
Usage
TODO