fp

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: MIT Imports: 0 Imported by: 27

README

fp-go

Go Reference Go Report codecov

Fp-go is a collection of Functional Programming helpers powered by Golang 1.18+ generics.

Contents

Install

Requires go 1.18+

go get github.com/repeale/fp-go

Features

Currying

By default! Data last!

func isPositive(x int) bool {
	return x > 0
}

func main() {
    filterPositive := fp.Filter(isPositive)
    numbers := []int{1, 2, 3, 4, 5}

    filterPositive(numbers)
}
Variations

Variations allows you to get different parameters in the callback function so that you get only only what is really needed.

Default
WithIndex
WithSlice

Default

Only the current item is available:

fp.Map[int, string](func(x int) { ... })
WithIndex

Current element and index are available:

fp.MapWithIndex[int, string](func(x int, i int) { ... })
WithSlice

Current element, index and the whole slice are available:

fp.MapWithSlice[int, string](func(x int, i int, xs: []int) { ... })

Helpers

Every

Determines whether all the members of an array satisfy the specified test.

Variations EveryWithIndex and EveryWithSlice

fp.Every(func(x int) bool { return x > 0 })([]int{1, 2, 3})

// => true
Filter

Returns the elements of an array that meet the condition specified in a callback function.

Variations FilterWithIndex and FilterWithSlice

fp.Filter(func(x int) bool { return x > 0 })([]int{-1, 2, -3, 4})

// => []int{2, 4}
Flat

Returns a new array with all sub-array elements concatenated into it.

fp.Flat([][]int{{1, 2}, {3, 4}})

// => []int{1, 2, 3, 4}
FlatMap

Calls a defined callback function on each element of an array. Then, flattens the result into a new array. This is identical to a map followed by flat.

Variations FlatMapWithIndex and FlatMapWithSlice

fp.FlatMap(func(x int) []int { return []int{x, x} })([]int{1, 2})

// => []int{1, 1, 2, 2}
Map

Calls a defined callback function on each element of an array, and returns an array that contains the results.

Variations MapWithIndex and MapWithSlice

fp.Map(func(x int64) string {
    return strconv.FormatInt(x, 10)
})([]int64{1, 2, 3})

// => []string{"1", "2", "3", "4"}
Reduce

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

Variations ReduceWithIndex and ReduceWithSlice

fp.Reduce(func(acc int, curr int) int { return acc + curr }, 0)([]int{1, 2, 3})

// => 6
Some

Determines whether the specified callback function returns true for any element of an array.

Variations SomeWithIndex and SomeWithSlice

fp.Some(func(x int) bool { return x < 0 })([]int{1, 2, 3})

// => false

Compose

Performs right-to-left function composition.

Variations Compose2, Compose3 and Compose4 stating the number of functions you are going to compose.

func isPositive(x int) bool {
	return x > 0
}

func sumTwo(x int) int {
	return x + 2
}

Compose2(fp.Filter(isPositive), fp.Map(sumTwo))([]int{1, 2, 3, -1})

// => []int{3,4,5,1}
Pipe

Performs left-to-right function composition.

Variations Pipe2, Pipe3 and Pipe4 stating the number of functions you are going to compose.

func isPositive(x int) bool {
	return x > 0
}

func sumTwo(x int) int {
	return x + 2
}

Pipe2(fp.Filter(isPositive), fp.Map(sumTwo))([]int{1, 2, 3, -1})

// => []int{3,4,5}
Curry

Allow to transfrom a function that receives a certain amount of params in single functions that take one single param each.

Variations Curry2, Curry3 and Curry4 stating the number of params will be curried individually.

curryedSum := Curry2(func(a int, b int) int { return a + b })
curryedSum(1)(2)

Structs

Option

Option represents encapsulation of an optional value, it might be used as the return type of functions which may or may not return a meaningful value when they are applied. You could instanciate an opt.Option[T] with a value with opt.Some(val). If the value is missing you can use opt.None[T]().

Option exports Some, None, IsSome, IsNone, GetOrElse, Match, Map, Chain.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compose2 added in v0.5.0

func Compose2[T1, T2, R any](fn2 func(T2) R, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of two functions

func Compose3 added in v0.5.0

func Compose3[T1, T2, T3, R any](fn3 func(T3) R, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of three functions

func Compose4 added in v0.5.0

func Compose4[T1, T2, T3, T4, R any](fn4 func(T4) R, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of four functions

func Curry2 added in v0.5.0

func Curry2[T1, T2, R any](fn func(T1, T2) R) func(T1) func(T2) R

Allow to transfrom a function that receives two params in single functions that take one single param each

func Curry3 added in v0.5.0

func Curry3[T1, T2, T3, R any](fn func(T1, T2, T3) R) func(T1) func(T2) func(T3) R

Like Curry2 but with three params

func Curry4 added in v0.5.0

func Curry4[T1, T2, T3, T4, R any](fn func(T1, T2, T3, T4) R) func(T1) func(T2) func(T3) func(T4) R

Like Curry2 but with four params

func Every added in v0.3.0

func Every[T any](predicate func(T) bool) func([]T) bool

Determines whether all the members of an array satisfy the specified test.

func EveryWithIndex added in v0.3.0

func EveryWithIndex[T any](predicate func(T, int) bool) func([]T) bool

See Every but callback receives index of element.

func EveryWithSlice added in v0.3.0

func EveryWithSlice[T any](predicate func(T, int, []T) bool) func([]T) bool

Like Every but callback receives index of element and the whole array.

func Filter added in v0.2.0

func Filter[T any](predicate func(T) bool) func([]T) []T

Returns the elements of an array that meet the condition specified in a callback function.

func FilterWithIndex added in v0.3.0

func FilterWithIndex[T any](predicate func(T, int) bool) func([]T) []T

See Filter but callback receives index of element.

func FilterWithSlice added in v0.3.0

func FilterWithSlice[T any](predicate func(T, int, []T) bool) func([]T) []T

Like Filter but callback receives index of element and the whole array.

func Flat added in v0.3.0

func Flat[T any](xs [][]T) []T

Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.

func FlatMap added in v0.3.0

func FlatMap[T any, R any](callback func(T) []R) func([]T) []R

Calls a defined callback function on each element of an array. Then, flattens the result into a new array. This is identical to a map followed by flat with depth 1.

func FlatMapWithIndex added in v0.3.0

func FlatMapWithIndex[T any, R any](callback func(T, int) []R) func([]T) []R

See FlatMap but callback receives index of element.

func FlatMapWithSlice added in v0.3.0

func FlatMapWithSlice[T any, R any](callback func(T, int, []T) []R) func([]T) []R

Like FlatMap but callback receives index of element and the whole array.

func Map

func Map[T any, R any](callback func(T) R) func([]T) []R

Calls a defined callback function on each element of an array, and returns an array that contains the results.

func MapWithIndex added in v0.3.0

func MapWithIndex[T any, R any](callback func(T, int) R) func([]T) []R

See Map but callback receives index of element.

func MapWithSlice added in v0.3.0

func MapWithSlice[T any, R any](callback func(T, int, []T) R) func([]T) []R

Like Map but callback receives index of element and the whole array.

func Pipe2 added in v0.5.0

func Pipe2[T1, T2, R any](fn1 func(T1) T2, fn2 func(T2) R) func(T1) R

Performs left-to-right function composition of two functions

func Pipe3 added in v0.5.0

func Pipe3[T1, T2, T3, R any](fn1 func(T1) T2, fn2 func(T2) T3, fn3 func(T3) R) func(T1) R

Performs left-to-right function composition of three functions

func Pipe4 added in v0.5.0

func Pipe4[T1, T2, T3, T4, R any](fn1 func(T1) T2, fn2 func(T2) T3, fn3 func(T3) T4, fn4 func(T4) R) func(T1) R

Performs left-to-right function composition of four functions

func Reduce added in v0.3.0

func Reduce[T any, R any](callback func(R, T) R, acc R) func([]T) R

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

func ReduceWithIndex added in v0.3.0

func ReduceWithIndex[T any, R any](callback func(R, T, int) R, acc R) func([]T) R

See Reduce but callback receives index of element.

func ReduceWithSlice added in v0.3.0

func ReduceWithSlice[T any, R any](callback func(R, T, int, []T) R, acc R) func([]T) R

Like Reduce but callback receives index of element and the whole array.

func Some added in v0.3.0

func Some[T any](predicate func(T) bool) func([]T) bool

Determines whether the specified callback function returns true for any element of an array.

func SomeWithIndex added in v0.3.0

func SomeWithIndex[T any](predicate func(T, int) bool) func([]T) bool

See Some but callback receives index of element.

func SomeWithSlice added in v0.3.0

func SomeWithSlice[T any](predicate func(T, int, []T) bool) func([]T) bool

Like Some but callback receives index of element and the whole array.

Types

type Lazy added in v0.6.0

type Lazy[T any] func() T

Callback function that returns a specific value type

type LazyVal added in v0.6.0

type LazyVal[T any] func(x T) T

Callback function that takes an argument and return a value of the same type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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