Categorygithub.com/repeale/fp-go
modulepackage
0.11.1
Repository: https://github.com/repeale/fp-go.git
Documentation: pkg.go.dev

# README

fp-go

Go Reference Go Report codecov

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

Inspired by

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}

    positives := 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 to Compose16 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 to Pipe16 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 to Curry16 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.

Option exports Some, None, IsSome, IsNone, Chain, Exists, Flatten, FromError, FromErrorFn, FromPredicate, GetOrElse, Map, Match.

Either

Either represent a value that can have two possible types. It is common to see Either used to represent a success value Right or a failure value Left, although that doesn't have to be the case. An instance of Either is either an instance of Left or Right.

Either exports Left, Right, IsLeft, IsRight, Exists, Flatten, FromError, FromErrorFn, FromOption, FromPredicate, GetOrElse, Map, MapLeft, Match,

# Packages

No description provided by the author
No description provided by the author

# Functions

Performs right-to-left function composition of 10 functions.
Performs right-to-left function composition of 11 functions.
Performs right-to-left function composition of 12 functions.
Performs right-to-left function composition of 13 functions.
Performs right-to-left function composition of 14 functions.
Performs right-to-left function composition of 15 functions.
Performs right-to-left function composition of 16 functions.
Performs right-to-left function composition of two functions.
Performs right-to-left function composition of three functions.
Performs right-to-left function composition of four functions.
Performs right-to-left function composition of 5 functions.
Performs right-to-left function composition of 6 functions.
Performs right-to-left function composition of 7 functions.
Performs right-to-left function composition of 8 functions.
Performs right-to-left function composition of 9 functions.
Allow to transform a function that receives 10 params in a sequence of unary functions.
Allow to transform a function that receives 11 params in a sequence of unary functions.
Allow to transform a function that receives 12 params in a sequence of unary functions.
Allow to transform a function that receives 13 params in a sequence of unary functions.
Allow to transform a function that receives 14 params in a sequence of unary functions.
Allow to transform a function that receives 15 params in a sequence of unary functions.
Allow to transform a function that receives 16 params in a sequence of unary functions.
Allow to transform a function that receives 2 params in a sequence of unary functions.
Allow to transform a function that receives 3 params in a sequence of unary functions.
Allow to transform a function that receives 4 params in a sequence of unary functions.
Allow to transform a function that receives 5 params in a sequence of unary functions.
Allow to transform a function that receives 6 params in a sequence of unary functions.
Allow to transform a function that receives 7 params in a sequence of unary functions.
Allow to transform a function that receives 8 params in a sequence of unary functions.
Allow to transform a function that receives 9 params in a sequence of unary functions.
Determines whether all the members of an array satisfy the specified test.
See Every but callback receives index of element.
Like Every but callback receives index of element and the whole array.
Returns the elements of an array that meet the condition specified in a callback function.
See Filter but callback receives index of element.
Like Filter but callback receives index of element and the whole array.
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Calls a defined callback function on each element of an array.
See FlatMap but callback receives index of element.
Like FlatMap but callback receives index of element and the whole array.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
See Map but callback receives index of element.
Like Map but callback receives index of element and the whole array.
Performs left-to-right function composition of 10 functions.
Performs left-to-right function composition of 11 functions.
Performs left-to-right function composition of 12 functions.
Performs left-to-right function composition of 13 functions.
Performs left-to-right function composition of 14 functions.
Performs left-to-right function composition of 15 functions.
Performs left-to-right function composition of 16 functions.
Performs left-to-right function composition of two functions.
Performs left-to-right function composition of three functions.
Performs left-to-right function composition of four functions.
Performs left-to-right function composition of five functions.
Performs left-to-right function composition of 6 functions.
Performs left-to-right function composition of 7 functions.
Performs left-to-right function composition of 8 functions.
Performs left-to-right function composition of 9 functions.
Calls the specified callback function for all the elements in an array.
See Reduce but callback receives index of element.
Like Reduce but callback receives index of element and the whole array.
Determines whether the specified callback function returns true for any element of an array.
See Some but callback receives index of element.
Like Some but callback receives index of element and the whole array.

# Type aliases

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