Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fogfish/golem

pure functional and generic programming for Go
https://github.com/fogfish/golem

algebraic-data-types functional-programming golang

Last synced: 2 days ago
JSON representation

pure functional and generic programming for Go

Awesome Lists containing this project

README

        



Golem


"Scrap Your Boilerplate" for Go




















sub-moduledocabout









Heterogenous sequence of types









Composable getters and setters (lenses) for manipulating data structures









Type safe channels and data pipeline combinator.









Generic purely functional abstractions (e.g. HKT, Monoid, etc).









Generic behavior traits.


---

> 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](./doc/okasaki.pdf) by Chris Okasaki, and implements various functional abstractions and patterns, deals with [scrap your boilerplate](https://www.microsoft.com/en-us/research/publication/scrap-your-boilerplate-with-class/) and benefits from the experience of other functional languages, primary Scala, Haskell and also heavily inspired by the Erlang library [datum](https://github.com/fogfish/datum). Golem is testing the limits of functional abstractions in Go.

## Inspiration

[Functional Programming](https://en.wikipedia.org/wiki/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](http://www.se-radio.net/2007/07/episode-62-martin-odersky-on-scala/) 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](https://blog.usejournal.com/solving-embarrassingly-obvious-problems-in-erlang-e3f21a6203cc)

> 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](https://blog.ploeh.dk/2019/11/04/the-80-24-rule/).

Functional style programming can be achieved in any language, including Go. Golang's [structural type system](https://en.wikipedia.org/wiki/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.

## Getting started

The library requires **Go 1.18** or later due to usage of [generics](https://go.dev/blog/intro-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.

1. Use `go get` to retrieve the library and add it as a dependency to your application.

```bash
go get -u github.com/fogfish/golem/{submodule}
```

2. Import required package in your code

```go
import (
"github.com/fogfish/golem/{submodule}"
)
```

## Examples and show cases
* [Type classes](doc/typeclass.md)
* [Higher-kinded polymorphism](doc/higher-kinded-polymorphism.md)
* [A Guide To Higher-Kinded Type Classes with Golang](https://towardsdev.com/a-guide-to-higher-kinded-type-classes-with-golang-36dab6c9ecc4)
* [Why higher-kinded polymorphism is vital functional abstraction and How to implement type classes with Golang](https://medium.com/@dmkolesnikov/why-higher-kinded-polymorphism-is-vital-functional-abstraction-and-how-to-implement-type-classes-b30ee0576dd5)
* [Type combinators](doc/combinator.md) deliver powerful patterns for functional programming.
* [A Guide To Pure Type Combinators in Golang or How to Stop Worrying and Love the Functional Programming](https://medium.com/@dmkolesnikov/a-guide-to-pure-type-combinators-in-golang-or-how-to-stop-worrying-and-love-the-functional-e14f7f8cf35c)
* [Monoid](doc/monoid.md) for structural transformation.
* [Abstract over Golang structure fields using optics](doc/abstract-over-struct-fields-using-optics.md)
* [Heterogenous Sequence of Types](/hseq/)
* [Golang Optics](/optics/)

## How To Contribute

The library is [MIT](LICENSE) licensed and accepts contributions via GitHub pull requests:

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

The build and testing process requires [Go](https://golang.org) version 1.18 or later.

**Build** and **run** in your development console.

```bash
git clone https://github.com/fogfish/golem
cd golem/{submodule}
go test ./...
```

## License

[![See LICENSE](https://img.shields.io/github/license/fogfish/golem.svg?style=for-the-badge)](LICENSE)