{"id":22946952,"url":"https://github.com/mickstanciu/go-fn","last_synced_at":"2025-10-11T00:38:54.184Z","repository":{"id":153099334,"uuid":"628156721","full_name":"MickStanciu/go-fn","owner":"MickStanciu","description":"GO Functional Programming","archived":false,"fork":false,"pushed_at":"2025-06-25T03:50:45.000Z","size":76,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-12T23:49:49.854Z","etag":null,"topics":["functional-programming","functors"],"latest_commit_sha":null,"homepage":"","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/MickStanciu.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":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-04-15T04:32:45.000Z","updated_at":"2025-06-25T03:48:04.000Z","dependencies_parsed_at":"2025-05-09T01:20:30.461Z","dependency_job_id":"cdefa896-3ac8-4fcb-8b83-99bbf4303855","html_url":"https://github.com/MickStanciu/go-fn","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/MickStanciu/go-fn","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MickStanciu%2Fgo-fn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MickStanciu%2Fgo-fn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MickStanciu%2Fgo-fn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MickStanciu%2Fgo-fn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MickStanciu","download_url":"https://codeload.github.com/MickStanciu/go-fn/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MickStanciu%2Fgo-fn/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279005647,"owners_count":26083942,"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","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["functional-programming","functors"],"created_at":"2024-12-14T14:49:10.046Z","updated_at":"2025-10-11T00:38:54.167Z","avatar_url":"https://github.com/MickStanciu.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-fn\n\n## How to install\n`go get github.com/MickStanciu/go-fn`\n\n## What is it?\nThis is a collection of useful small Go functions that are using generics\n\n## How to use\n### Filters\nstatement which can be evaluated to true/false\n```go\ntype Predicate[T any] func(T) bool\n```\n\n#### Filter Slice\nwill filter a collection based on the provided predicate\n```go\nfunc FilterSliceBy[T any](input []T, p Predicate[T]) []T\n```\n\nExample:\n```go\nevens := fn.FilterSliceBy([]int{1, 2, 3, 4, 5, 6, 7, 8}, func(i int) bool {\n    return i%2 == 0\n})\n```\n\n#### Filter Map\nwill filter a map based on the provided predicate\n```go\nfunc FilterMapBy[KEY string, U any](input map[KEY]U, p Predicate[U]) map[KEY]U \n```\n\nExample:\n```go\ngreaterThanTen := FilterMapBy(map[string]int{\"a\": 1, \"b\": 2, \"c\": 30}, func(i int) bool {\n    return i \u003e 10\n})\n```\n\n#### Any\nreturns true if one element satisfies the predicate function\n```go\nfunc Any[T any](input []T, p Predicate[T]) bool\n```\n\nExample:\n```go\nresult := fn.Any[string]([]string{\"A\", \"B\", \"C\"}, func(s string) bool {\n    return s == \"A\"\n})\n```\n\n#### All\nreturn true if all elements are satisfying the predicate function\n```go\nfunc All[T any](input []T, p Predicate[T]) bool\n```\n\nExample:\n```go\nresult := fn.All[string]([]string{\"A\", \"A\", \"A\"}, func(s string) bool {\n    return s == \"A\"\n})\n```\n\n#### GetOrElse\nwill return the input if the predicate is satisfied, otherwise will return the `else`\n```go\nfunc GetOrElse[T any](input T, other T, p Predicate[T]) T\n```\n\nExample:\n```go\nres := fn.GetOrElse(10, 20, func(i int) bool {\n    return i \u003e 15\n})\n```\n\n#### DropWhileRight\nfilters a collection of type T, using a predicate function, by removing the right-most elements which satisfy the predicate, while preserving the order\n\n```go\nfunc DropWhileRight[T any](input []T, p Predicate[T]) []T\n```\n\nExample: \n```go\nresult := fn.DropWhileRight([]string{\"A\", \"B\", \"C\"}, func(s string) bool {\n    return s == \"C\"\n})\n```\n\n#### TakeAll\nfilters a collection of type T, using a predicate function, returning the elements which satisfy the predicate\n\n```go\nfunc TakeAll[T any](input []T, p Predicate[T]) []T\n```\n\nExample:\n```go\nresult := fn.TakeAll([]string{\"A\",\"B\",\"C\",\"D\",\"E\"}, func(s string) bool {\n    return strings.HasPrefix(s, \"D\")\n})\n```\n\n#### DeDuplicateList\nmakes sure the elements in a collection are unique, based on string KEY\n\n```go\nfunc DeduplicateList[T any](elements []*T, pkFun func(element *T) string) []*T\n```\n\nExample:\n```go\ntype human struct {\n    ID   string\n    Name string\n    Age  int\n}\n\nresult := fn.DeduplicateList(tt.input, func(element *human) string {\n    return element.ID\n})\n```\n\n#### DeDuplicateOrderedList\nmakes sure the elements in a collection are unique, based on string KEY, preserves the original order\n\n```go\nfunc DeduplicateOrderedList[T any](elements []*T, pkFun func(element *T) string) []*T\n```\n\n\n### Maps\ntransformation functions\n```go\ntype MapFn[A, B any] func(A) B\n````\n\n#### TransformSliceBy\napplies a transformation function A -\u003e B to each element of type A\n```go\nfunc TransformSliceBy[A, B any](input []A, fn MapFn[A, B]) []B\n```\n\n```go\nfn.Fmap([]string{\"George\", \"Maria\", \"John\"}, func(a string) string {\n    return \"Hello \" + a \n})\n```\n\n#### TransformMapBy\napplies a transformation function A -\u003e B to each element of type A\n\n```go\nTransformMapBy[A, B any](input map[string]A, fn MapFn[A, B]) map[string]B\n```\n\nExample:\n```go\nmakeItDouble := TransformMapBy(map[string]int{\"a\": 1, \"b\": 2, \"c\": 30}, func(i int) int {\n\t\treturn i * 2\n\t})\n```\n\n#### FlatMap\napplies a transformation function from T to []T to each element of type T\n```go\nresult := fn.FlatMap([]int{1, 2, 3}, func(i int) []int {\n    var out []int\n    for j := 0; j \u003c i; j++ {\n        out = append(out, j)\n    }\n    return out\n})\n```\n\n#### Reduce\nwill fold a collection\n```go\nfunc Reduce[T any](input []T, fn ReduceFn[T]) T\n```\n\nExample:\n```go\nfn.Reduce([]int{1, 2, 3}, func(a, b int) int {\n    return a + b + 1\n})\n```\nresult is `7`\n\n#### Reduce And Compute\nwill fold a collection into a number\n```go\ntype ComputeFn[T, R any] func(T, R) R\nfunc ReduceAndCompute[T any, R Number](input []T, fn ComputeFn[T, R]) R\n```\n\nExample:\n```go\ntype cat struct {\n    weight int\n}\nsumOfAllCats := fn.ReduceAndCompute(test.input, func(a cat, b int) int {\n    return a.weight + b\n})\n```\n\n#### Zip\nwill combine 2 collections\n```go\nfunc Zip[A, B, C any](a []A, b []B, fn func(A, B) C) []C\n```\n\nExample:\n```go\nx := []string{\"a\", \"b\", \"c\"}\ny := []int{1, 2, 3}\nfn.Zip(x, y, func(a string, b int) string {\n    return fmt.Sprintf(\"%s-%d\", a, b)\n}\n```\n\n#### SplitSliceInBatch\nwill split a slice into batches and then will call the callback function for each batch.\n```go\nSplitSliceInBatch[T any](size int, collection []T, fn func(batch []T) error) error\n```\n\nExample:\n```go\nerr := batch.SplitSliceInBatch(2, []string{\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"},\n    func(strings []string) error {\n        if strings[0] == \"d\" {\n            return fmt.Errorf(\"error in processing function\")\n        }\n        return nil\n    })\n```\n\n#### ParallelExecByKey\nwill batch execute a given function, where items are identified by a key\n```go\nfunc ParallelExecByKey[R any, Key string](ctx context.Context, batchSize int, keys []Key, fn func(ctx context.Context, key Key) (R, error)) (map[Key]R, error)\n```\n\nExample:\n```go\nres, err := batch.ParallelExecByKey(\n\t\tctx, 2,\n\t\t[]string{\"item_1\", \"item_2\", \"item_3\", \"item_4\", \"item_5\", \"item_6\"},\n\t\tfunc(ctx context.Context, itemID string) (string, error) {\n\t\t\treturn fmt.Sprintf(\"%s_\", itemID), nil\n\t\t},\n\t)\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmickstanciu%2Fgo-fn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmickstanciu%2Fgo-fn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmickstanciu%2Fgo-fn/lists"}