MIT License

项目介绍

repeale/fp-go 是一个托管在 GitHub 上的开源项目,主要使用 Go 编写,采用 MIT License 许可证,目前获得 323 个 Star、9 次 Fork。

仓库全名
repeale/fp-go
开源许可证
MIT License
Star 数
323
Fork 数
9
未关闭 Issue
1

README 摘要

fp-go Fp-go is a collection of Functional Programming helpers powered by Golang 1.18+ generics. Inspired by fp-ts width="500" height="313" src="https://user-images.githubusercontent.com/9580458/162070974-4367f4b8-bb3d-451c-8114-dd578bad4e46.png" Contents Install Features Currying Variations Helpers Every Filter Flat FlatMap Map Reduce Some Compose Pipe Curry Structs Option Either Install Requires go 1.18+ Features…

完整内容见 GitHub 原始 README 或本页 。

常见问题

fp-go 用什么语言开发?

主要使用 Go 编写,采用 MIT License 许可证,目前有 323 个 Star。

fp-go 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 fp-go 的源码?

可以直接克隆仓库:git clone https://github.com/repeale/fp-go.git,默认分支为 main;安装与运行步骤见仓库 README。

fp-go 最近还在维护吗?

仓库最近一次更新为 2022年12月2日,当前有 1 个未关闭 Issue;Star 数为 323,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

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

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
346
价值分
196
活跃状态
活跃

语言占比

Go
Python

Release

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

评论

暂无评论