Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/BooleanCat/go-functional
go-functional is a library of iterators to augment the standard library
https://github.com/BooleanCat/go-functional
functional-programming go golang
Last synced: about 2 months ago
JSON representation
go-functional is a library of iterators to augment the standard library
- Host: GitHub
- URL: https://github.com/BooleanCat/go-functional
- Owner: BooleanCat
- License: mit
- Created: 2018-03-06T18:17:44.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-07-27T08:36:06.000Z (about 2 months ago)
- Last Synced: 2024-07-27T09:52:13.739Z (about 2 months ago)
- Topics: functional-programming, go, golang
- Language: Go
- Homepage:
- Size: 466 KB
- Stars: 354
- Watchers: 8
- Forks: 22
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
- awesome-go - go-functional - Functional programming in Go using generics (Functional / Search and Analytic Databases)
- awesome-ccamel - BooleanCat/go-functional - go-functional is a library of iterators to augment the standard library (Go)
README
# Functional Programming in Go
[![GitHub release (with filter)](https://img.shields.io/github/v/release/BooleanCat/go-functional?sort=semver&logo=Go&color=%23007D9C&include_prereleases)](https://github.com/BooleanCat/go-functional/releases) [![Actions Status](https://github.com/BooleanCat/go-functional/workflows/test/badge.svg)](https://github.com/BooleanCat/go-functional/actions) [![Go Reference](https://pkg.go.dev/badge/github.com/BooleanCat/go-functional/v2.svg)](https://pkg.go.dev/github.com/BooleanCat/go-functional/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/BooleanCat/go-functional/v2)](https://goreportcard.com/report/github.com/BooleanCat/go-functional/v2) [![codecov](https://codecov.io/gh/BooleanCat/go-functional/branch/main/graph/badge.svg?token=N2E43RSR14)](https://codecov.io/gh/BooleanCat/go-functional)
A library of iterators for use with `iter.Seq`. Requires Go 1.23+.
```go
// The first 5 natural numbers
numbers := slices.Collect(
it.Take(it.NaturalNumbers[int](), 5),
)// All even numbers
evens := it.Filter(it.NaturalNumbers[int](), filter.IsEven)// String representations of integers
numbers := it.Map(it.NaturalNumbers[int](), strconv.Itoa)
```_[Read the docs](https://pkg.go.dev/github.com/BooleanCat/go-functional/v2)_ to see the full iterator library.
## Installation
```terminal
go get github.com/BooleanCat/go-functional/v2@latest
```## Iterator Chaining
The iterators in this package were designed to be used with the native
`iter.Seq` from Go's standard library. In order to facilitate complex
sequences of iterators, the
[`itx`](https://github.com/BooleanCat/go-functional/tree/main/it/itx) package
provides `Iterator` and `Iterator2` as wrappers around `iter.Seq` and
`iter.Seq2` that allow for chaining operations.Let's take a look at an example:
```go
// The first 10 odd integers
itx.NaturalNumbers[int]().Filter(filter.IsOdd).Take(10).Collect()
```Most iterators support chaining. A notable exception is `it.Map` which cannot
support chaining due to limitations on Go's type system.