fp

package module
v0.0.0-...-edf41e2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

Go Functional Programming

This library is cursed and you probably shouldn't use it. It relies on the Dark Arts to enable a very non-idoimatic way of writing Go.

I was inspired by @fossker's wonderful talk at Doomconf 2022H2 and his Common Lisp Transducers package and documentation.

But this is a different approach that aims to build upon Go's existing standard library to introduce transducers in a simple way alongside a lot of other FP features that I've wanted across various projects.

There are many FP/utility libraries for Go. This one is mine.

The end result is the ability to write Go code that looks like this

Transduce(
	Integers(),
	Chain4(
		Curry2(Take[int], 5),
		Delta,
		Curry2(Map, func(i int) []byte {
			return []byte(strconv.Itoa(i))
		}),
		Visitor(Curry2(Write, io.Writer(&buf))),
	),
	Collect,
)

None of the ideas in this library are new. I'm mostly copying features from the wonderful Clojure and of course Rust.

There are also some other kinda misc things in this library. As its grown it's become more of a general purpose utility library that I use. You can easily import just the parts you want.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GlobalErrorHandler = func(err error) bool {
	panic(err)
}

Functions

func Assert

func Assert(t *testing.T, cond bool)

func AssertEq

func AssertEq[T comparable](t *testing.T, a, b T)

func AssertSliceEq

func AssertSliceEq[S ~[]E, E comparable](t *testing.T, a, b S)

func CastOr

func CastOr[T any](x any, or T) T

func Check

func Check(err error)

Check is the second

func Clamp

func Clamp[T cmp.Ordered](x T, lo T, hi T) T

func DerefOr

func DerefOr[T any](ref *T, or T) T

DerefOr dereferences the passed pointer otherwise returns the or value

func DerefZero

func DerefZero[T any](ref *T) T

DerefZero is like DerefOr but returns the zero value if nil

func Must

func Must[T any](t T, err error) T

Must is the first function anyone wants in Go

func Or

func Or[T comparable](vals ...T) T

Or re-exports cmp.Or

func Ptr

func Ptr[T any](t T) *T

Ptr returns a pointer of its argument

func Pull

func Pull[V any](seq Seq[V]) (next func() (V, bool), stop func())

Pull is a wrappr around iter.Pull

func Pull2

func Pull2[K comparable, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func())

Pull2 is a wrapper around iter.Pull2

func Ternary

func Ternary[T any](cond bool, a T, b T) T

Types

type Complex

type Complex = constraints.Complex

type Float

type Float = constraints.Float

type Integer

type Integer = constraints.Integer

type KeyValue

type KeyValue[K comparable, V any] struct {
	Key   K
	Value V
}

KeyValue is a key-value pair

type Nilable

type Nilable interface {
	~*any | ~[]any | ~map[any]any
}

type Numeric

type Numeric interface {
	Integer | Float
}

type Ordered

type Ordered = constraints.Ordered

type Seq

type Seq[V any] interface {
	// Seq implements push-style iteration using the yield callback.
	// See the documenation of [iter] for more information.
	// It has exactly the same signature as [SeqFunc].
	// This function can be used directly with the for-range statement
	Seq(yield func(V) bool)
}

Seq borrows a trick used by [http.Handler] to define an interface and a func that implements that interface by calling itself SeqFunc

type Seq2

type Seq2[K comparable, V any] interface {
	// I don't like the whole [iter.Seq2] thing that the stdlib does
	// so we use this to convert [Seq2] into [Seq]
	// This trivially gives compatibility with the rest of this library
	Seq(yield func(KeyValue[K, V]) bool)
	Seq2(yield func(K, V) bool)
}

Seq2 is to Seq what iter.Seq is to iter.Seq2 but with the additional requirement that Seq2 implements Seq

func Duet

func Duet[K comparable, V any](seq Seq[KeyValue[K, V]]) Seq2[K, V]

Duet is the inverse of [Seq2.Seq] taking a Seq of KeyValue and returning the Seq2 equivalent

type Seq2Func

type Seq2Func[K comparable, V any] iter.Seq2[K, V]

Seq2Func is exactly the same as iter.Seq2 and can be trivially cast between.

func (Seq2Func[K, V]) Seq

func (sf Seq2Func[K, V]) Seq(yield func(KeyValue[K, V]) bool)

func (Seq2Func[K, V]) Seq2

func (sf Seq2Func[K, V]) Seq2(yield func(K, V) bool)

type SeqFunc

type SeqFunc[V any] iter.Seq[V]

SeqFunc is exactly the same as iter.Seq and can be trivially cast between

func (SeqFunc[V]) Seq

func (sf SeqFunc[V]) Seq(yield func(V) bool)

type Signed

type Signed = constraints.Signed

type Unsigned

type Unsigned = constraints.Unsigned

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL