ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - repeale/fp-go: fp-go is a collection of Functional Programming helpers powered by Golang 1.18+ generics.
fp-go is a collection of Functional Programming helpers powered by Golang 1.18+ generics. - repeale/fp-go
Visit Site

GitHub - repeale/fp-go: fp-go is a collection of Functional Programming helpers powered by Golang 1.18+ generics.

GitHub - repeale/fp-go: fp-go is a collection of Functional Programming helpers powered by Golang 1.18+ generics.

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"}

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,

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory