funutils

package module
v0.0.0-...-77f5a60 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 1 Imported by: 0

README

funutils

Add functional programming to your Go code

Package funutils includes functions implementing common functional programming concepts and patterns. The name was inspired from an R documentation page named funprog. (I am not the only one who sees the "fun" in the "functional")

The package includes functions like:

  • map
  • filter
  • reduce
  • composition
  • ... and many more

NOTE: This package is a simple collection of utility functions. For a more fleshed out FP implementation, check out fp-go.

Installation

Following the Go proverb "a little copying is better than a little dependency," I encourage you to browse code and if you only need a function or two from here, just copy them into your project.

If you decide you want the whole package, simply use go get in your project directory:

go get github.com/wipdev-tech/funprog

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](p func(T) bool, s []T) bool

Any takes a predicate `p` and a slice `s` and returns true if all element of `s` satisfy `p` (and false otherwise).

func Any

func Any[T any](p func(T) bool, s []T) bool

Any takes a predicate `p` and a slice `s` and returns true if any element of `s` satisfies `p` (and false otherwise).

func Comp

func Comp[T any](fs ...func(T) T) func(T) T

Comp is a high-order function that implements composition. Given an arbitrary number of functions as input, Comp will return a function so that Comp(f, g)(x) == f(g(x)).

This is an initial implementation which works only on functions that return the same type as their input.

func Comp2

func Comp2[TIn any, TMid any, TOut any](f func(TMid) TOut, g func(TIn) TMid) func(TIn) TOut

Comp2 is a high-order function that implements composition. Given two functions as input, it will return a function so that Comp2(f, g)(x) == f(g(x)).

When composing two functions, this one is more preferable to CompAll as it has less general type constraints.

func CompR

func CompR[T any](fs ...func(T) T) func(T) T

CompR is the inverse of Comp. Given an arbitrary number of functions as input, Comp will return a function so that Comp(f, g)(x) == g(f(x)).

This is an initial implementation which works only on functions that return the same type as their input.

func Curry

func Curry()

func Filter

func Filter[T any](p func(T) bool, s []T) []T

Filter implements the common high-order function `filter`. It takes a function (predicate) and a slice of some type, and it returns a slice containing the elements of the input slice for which the predicate returns true.

func FindAll

func FindAll[T any](p func(T) bool, s []T) []int

func FindIndex

func FindIndex[T any](p func(T) bool, s []T) int

FindIndex takes a predicate `p` and a slice `s` and returns the index of the first element that satisfies `p`. It returns -1 if no element is found.

See FindIndices if you want to return all indices.

func FindIndices

func FindIndices[T any](p func(T) bool, s []T) []int

FindIndices takes a predicate `p` and a slice `s` and returns an int slice of all the indices of the elements that satisfy `p`.

See FindIndex if you want to return the first index only.

func Map

func Map[TIn any, TOut any](f func(TIn) TOut, s []TIn) []TOut

Map implements the common high-order function `map`. It takes a function and a slice of some type, applies the function on each element of the slice, and returns a new slice of the same type.

func PartApply

func PartApply()

func Reduce

func Reduce[TIn any, TAcc any](f func(TAcc, TIn) TAcc, s []TIn) TAcc

Reduce implements the common high-order function `reduce`. It takes a function and a slice. The input function must have two parameters so that the first one would be the "accumulator" and the second would be the next element in the slice.

Note that the initial value of the accumulator is set to the zero value of whatever type (T) returned by Reduce.

Types

This section is empty.

Jump to

Keyboard shortcuts

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