Fp-go is a collection of Functional Programming helpers powered by Golang 1.18+ generics.
Inspired by
Requires go 1.18+
go get github.com/repeale/fp-go
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 allows you to get different parameters in the callback function so that you get only only what is really needed.
Only the current item is available:
fp.Map[int, string](func(x int) { ... })
Current element and index are available:
fp.MapWithIndex[int, string](func(x int, i int) { ... })
Current element, index and the whole slice are available:
fp.MapWithSlice[int, string](func(x int, i int, xs: []int) { ... })
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
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}
Returns a new array with all sub-array elements concatenated into it.
fp.Flat([][]int{{1, 2}, {3, 4}})
// => []int{1, 2, 3, 4}
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}
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"}
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
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
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}
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}
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)
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 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
,