neocastro / golem

pure functional and generic programming for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Golem

"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 pure functional and generic programming for Go. It had its origins in Purely Functional Data Structures by Chris Okasaki, on implementing a various higher rank functional abstractions and patterns, on dealing with scrap your boilerplate and gaining experience from other functional languages primary Scala, Haskell and heavily inspired by Erlang twin library datum. Golem is testing the limits of functional abstractions in Go.

Inspiration

Functional Programming is a declarative style of development that uses side-effect free functions to express 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 a 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 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 highly likely you are solving few problems without articulating them. A 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, Go's structures, arrays, slices and maps embrace mutability rather than restricting it. Scala is a good example of the language that uses imperative runtime but provide data structure implementations that internally prevent 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.

Key features

Getting started

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.

  1. Use go get to retrieve the library and add it as dependency to your application.
go get -u github.com/fogfish/golem
  1. Import required package in your code
import (
  "github.com/fogfish/golem/..." // <-- path-to-package
)

Package

See the library documentation

Type traits

  • pure/eq is Eq (equality) type trait
  • pure/foldable is Foldable type trait define rules of folding data structures to a summary value.
  • pure/monoid is Monoid type trait defined an algebraic structure consisting of Semigroup and Empty element.
  • pure/ord is Ord (ordering) type trait
  • pure/semigroup is Semigroup type trait defined an associative binary operation for a set.

Data Type traits

  • maplike is MapLike type trait
  • seq is Seq type trait

Data Structures

How To Contribute

The library is MIT 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 version 1.13 or later.

Build and run in your development console.

git clone https://github.com/fogfish/golem
cd golem
go test -cover ./...

License

See LICENSE

About

pure functional and generic programming for Go

License:MIT License


Languages

Language:Go 100.0%