{"id":13412898,"url":"https://github.com/repeale/fp-go","last_synced_at":"2025-04-06T01:34:02.960Z","repository":{"id":38413891,"uuid":"466879352","full_name":"repeale/fp-go","owner":"repeale","description":"fp-go is a collection of Functional Programming helpers powered by Golang 1.18+ generics.","archived":false,"fork":false,"pushed_at":"2022-12-01T21:31:08.000Z","size":42,"stargazers_count":310,"open_issues_count":4,"forks_count":11,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-20T03:04:06.224Z","etag":null,"topics":["currying","functional","functional-programming","go","golang","option"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/repeale/fp-go","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/repeale.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-06T23:09:02.000Z","updated_at":"2025-03-10T14:09:06.000Z","dependencies_parsed_at":"2023-01-22T11:15:31.081Z","dependency_job_id":null,"html_url":"https://github.com/repeale/fp-go","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/repeale%2Ffp-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/repeale%2Ffp-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/repeale%2Ffp-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/repeale%2Ffp-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/repeale","download_url":"https://codeload.github.com/repeale/fp-go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247423460,"owners_count":20936621,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["currying","functional","functional-programming","go","golang","option"],"created_at":"2024-07-30T20:01:30.794Z","updated_at":"2025-04-06T01:34:02.939Z","avatar_url":"https://github.com/repeale.png","language":"Go","funding_links":[],"categories":["Functional","Go","方法"],"sub_categories":["Search and Analytic Databases","检索及分析资料库","Advanced Console UIs"],"readme":"# fp-go\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/repeale/fp-go.svg)](https://pkg.go.dev/github.com/repeale/fp-go)\n[![Go Report](https://goreportcard.com/badge/github.com/repeale/fp-go)](https://goreportcard.com/badge/github.com/repeale/fp-go)\n[![codecov](https://codecov.io/gh/repeale/fp-go/branch/main/graph/badge.svg?token=ORP8NR634Q)](https://codecov.io/gh/repeale/fp-go)\n\nFp-go is a collection of Functional Programming helpers powered by Golang [1.18](https://tip.golang.org/doc/go1.18)+ [generics](https://tip.golang.org/doc/go1.18#generics).\n\n**Inspired by**\n\n- [fp-ts](https://github.com/gcanti/fp-ts)\n\n\u003cp align=\"center\"\u003e\n  \u003cimg\n    width=\"500\"\n    height=\"313\"\n    src=\"https://user-images.githubusercontent.com/9580458/162070974-4367f4b8-bb3d-451c-8114-dd578bad4e46.png\"\n  \u003e\n\u003c/p\u003e\n\n## Contents\n\n- [Install](#install)\n- [Features](#features)\n  - [Currying](#currying)\n  - [Variations](#variations)\n- [Helpers](#helpers)\n  - [Every](#every)\n  - [Filter](#filter)\n  - [Flat](#flat)\n  - [FlatMap](#flatmap)\n  - [Map](#map)\n  - [Reduce](#reduce)\n  - [Some](#some)\n  - [Compose](#compose)\n  - [Pipe](#pipe)\n  - [Curry](#curry)\n- [Structs](#structs)\n  - [Option](#option)\n  - [Either](#either)\n\n## Install\n\nRequires go 1.18+\n\n```sh\ngo get github.com/repeale/fp-go\n```\n\n## Features\n\n- [Currying](#currying)\n- [Variations](#variations)\n\n### Currying\n\nBy default! Data last!\n\n```go\nfunc isPositive(x int) bool {\n\treturn x \u003e 0\n}\n\nfunc main() {\n    filterPositive := fp.Filter(isPositive)\n    numbers := []int{1, 2, 3, 4, 5}\n\n    positives := filterPositive(numbers)\n}\n```\n\n### Variations\n\nVariations allows you to get different parameters in the callback function so that you get only only what is really needed.\n\n[Default](#default) \\\n[WithIndex](#withindex) \\\n[WithSlice](#withslice)\n\n#### Default\n\nOnly the current item is available:\n\n```go\nfp.Map[int, string](func(x int) { ... })\n```\n\n#### WithIndex\n\nCurrent element and index are available:\n\n```go\nfp.MapWithIndex[int, string](func(x int, i int) { ... })\n```\n\n#### WithSlice\n\nCurrent element, index and the whole slice are available:\n\n```go\nfp.MapWithSlice[int, string](func(x int, i int, xs: []int) { ... })\n```\n\n## Helpers\n\n#### Every\n\nDetermines whether all the members of an array satisfy the specified test.\n\nVariations `EveryWithIndex` and `EveryWithSlice`\n\n```go\nfp.Every(func(x int) bool { return x \u003e 0 })([]int{1, 2, 3})\n\n// =\u003e true\n```\n\n#### Filter\n\nReturns the elements of an array that meet the condition specified in a callback function.\n\nVariations `FilterWithIndex` and `FilterWithSlice`\n\n```go\nfp.Filter(func(x int) bool { return x \u003e 0 })([]int{-1, 2, -3, 4})\n\n// =\u003e []int{2, 4}\n```\n\n#### Flat\n\nReturns a new array with all sub-array elements concatenated into it.\n\n```go\nfp.Flat([][]int{{1, 2}, {3, 4}})\n\n// =\u003e []int{1, 2, 3, 4}\n```\n\n#### FlatMap\n\nCalls 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.\n\nVariations `FlatMapWithIndex` and `FlatMapWithSlice`\n\n```go\nfp.FlatMap(func(x int) []int { return []int{x, x} })([]int{1, 2})\n\n// =\u003e []int{1, 1, 2, 2}\n```\n\n#### Map\n\nCalls a defined callback function on each element of an array, and returns an array that contains the results.\n\nVariations `MapWithIndex` and `MapWithSlice`\n\n```go\nfp.Map(func(x int64) string {\n    return strconv.FormatInt(x, 10)\n})([]int64{1, 2, 3})\n\n// =\u003e []string{\"1\", \"2\", \"3\"}\n```\n\n#### Reduce\n\nCalls 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.\n\nVariations `ReduceWithIndex` and `ReduceWithSlice`\n\n```go\nfp.Reduce(func(acc int, curr int) int { return acc + curr }, 0)([]int{1, 2, 3})\n\n// =\u003e 6\n```\n\n#### Some\n\nDetermines whether the specified callback function returns true for any element of an array.\n\nVariations `SomeWithIndex` and `SomeWithSlice`\n\n```go\nfp.Some(func(x int) bool { return x \u003c 0 })([]int{1, 2, 3})\n\n// =\u003e false\n```\n\n---\n\n#### Compose\n\nPerforms right-to-left function composition.\n\nVariations `Compose2` to `Compose16` stating the number of functions you are going to compose.\n\n```go\nfunc isPositive(x int) bool {\n\treturn x \u003e 0\n}\n\nfunc sumTwo(x int) int {\n\treturn x + 2\n}\n\nCompose2(fp.Filter(isPositive), fp.Map(sumTwo))([]int{1, 2, 3, -1})\n\n// =\u003e []int{3, 4, 5, 1}\n```\n\n#### Pipe\n\nPerforms left-to-right function composition.\n\nVariations `Pipe2` to `Pipe16` stating the number of functions you are going to compose.\n\n```go\nfunc isPositive(x int) bool {\n\treturn x \u003e 0\n}\n\nfunc sumTwo(x int) int {\n\treturn x + 2\n}\n\nPipe2(fp.Filter(isPositive), fp.Map(sumTwo))([]int{1, 2, 3, -1})\n\n// =\u003e []int{3, 4, 5}\n```\n\n#### Curry\n\nAllow to transfrom a function that receives a certain amount of params in single functions that take one single param each.\n\nVariations `Curry2` to `Curry16` stating the number of params will be curried individually.\n\n```go\ncurryedSum := Curry2(func(a int, b int) int { return a + b })\ncurryedSum(1)(2)\n```\n\n## Structs\n\n#### Option\n\nOption 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.\n\nOption exports `Some`, `None`, `IsSome`, `IsNone`, `Chain`, `Exists`, `Flatten`, `FromError`, `FromErrorFn`, `FromPredicate`, `GetOrElse`, `Map`, `Match`.\n\n#### Either\n\nEither 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.\nAn instance of `Either` is either an instance of `Left` or `Right`.\n\nEither exports `Left`, `Right`, `IsLeft`, `IsRight`, `Exists`, `Flatten`, `FromError`, `FromErrorFn`, `FromOption`, `FromPredicate`, `GetOrElse`, `Map`, `MapLeft`, `Match`,\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frepeale%2Ffp-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frepeale%2Ffp-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frepeale%2Ffp-go/lists"}