fp

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2022 License: MIT Imports: 0 Imported by: 27

README

fp-go - Functional programming helpers

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
Filter
Flat
FlatMap
Map
Reduce
Some

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

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

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

// => []int{1, 2, 3, 4}
FlatMap
fp.FlatMap(func(x int) []int { return []int{x, x} })([]int{1, 2})

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

// => []string{"1", "2", "3", "4"}
Reduce
fp.Reduce(func(acc int, curr int) int { return acc + curr }, 0)([]int{1, 2, 3})

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

// => false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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

func EveryWithSlice added in v0.3.0

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

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

func FilterWithSlice added in v0.3.0

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

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

func FlatMapWithSlice added in v0.3.0

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

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

func MapWithSlice added in v0.3.0

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

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

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

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

func SomeWithSlice added in v0.3.0

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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