"Scrap Your Boilerplate" for Go
His dust was "kneaded into a shapeless husk."
You could do this with a macro, but... the best macro is a macro you don't maintain
Golem is a purely functional and generic programming library for Go. It has its origins in Purely Functional Data Structures by Chris Okasaki, and implements various functional abstractions and patterns, deals with scrap your boilerplate and benefits from the experience of other functional languages, primary Scala, Haskell and also heavily inspired by the Erlang library datum. Golem is testing the limits of functional abstractions in Go.
Functional Programming is a declarative style of development that uses side effect free functions to express the solution of the problem domain. The core concepts of functional programming are elaborated by Martin Odersky - First class and high-order functions and immutability. Another key feature in functional programming is the composition - a style of development to build new things from small reusable elements. Functional code looks great only if functions clearly describe your problem. Usually, lines of code per function is only a single metric that reflects the quality of the code
If your functions have more than a few lines of code (a maximum of four to five lines per function is a good benchmark), you need to look more closely — chances are you have an opportunity to factor them into smaller, more tightly focused functions
This is because functions describe the solution to your problem. If your code contains many lines, then it is highly likely, that you are solving few problems without explicitly articulating them. And critical thinking is the process of software development - Write small blocks of code.
Functional style programming can be achieved in any language, including Go. Golang's structural type system helps to reject invalid programs at compilation time. One of the challenge here is, that Go's structures, arrays, slices and maps embrace mutability rather than restricting it. Scala is a good example of the language that uses an imperative runtime but provides data structure implementations that inherently avoid mutation. This is a perfect approach to achieve immutability and performance through well-defined scopes. All-in-all, Go is a general purpose language with simple building blocks. This library uses these blocks to implement a functional style of development with the goal of simplicity in mind.
The library requires Go 1.18 or later due to usage of generics.
The latest version of the library is available at main
branch. All development, including new features and bug fixes, take place on the main
branch using forking and pull requests as described in contribution guidelines. The stable version is available via Golang modules.
- Use
go get
to retrieve the library and add it as a dependency to your application.
go get -u github.com/fogfish/golem/{submodule}
- Import required package in your code
import (
"github.com/fogfish/golem/{submodule}"
)
- Type classes
- Higher-kinded polymorphism
- A Guide To Higher-Kinded Type Classes with Golang
- Why higher-kinded polymorphism is vital functional abstraction and How to implement type classes with Golang
- Type combinators deliver powerful patterns for functional programming.
- A Guide To Pure Type Combinators in Golang or How to Stop Worrying and Love the Functional Programming
- Monoid for structural transformation.
- Abstract over Golang structure fields using optics
- Heterogenous Sequence of Types
- Golang Optics
The library is MIT licensed and accepts contributions via GitHub pull requests:
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
The build and testing process requires Go version 1.18 or later.
Build and run in your development console.
git clone https://github.com/fogfish/golem
cd golem/{submodule}
go test ./...