Functional Patterns in Golang
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:
All these modules use the newly added features of Golang v1.18 (Still in beta as of today) notably type and function generics.
go get github.com/medmouine/gomad/<Desired module> i.e go get github.com/medmouine/gomad/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. This is helpful for CRUD operations by simplifying the code and allowing for seamless manipulation of nullable data.
You can use the functions
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)
Allows to manipulate pairs of mutually exclusive data. For example, if we would want to fall back to a value B if A answers to a specific predicate. This interface allows integrating this behavior seamlessly by abstracting all the underlying logic of managing both values. A common use case for this is form validation for front-end applications.
This interface aim at abstracting all logic related to operations susceptible to failures, such as external API calls, etc. It offers constructors and methods to safely manipulate the result in case of success and handle errors gracefully in case of failure.