https://github.com/rakeeb-hossain/functools
Functional tools in Go 1.18 using newly introduced generics
https://github.com/rakeeb-hossain/functools
functional generics go golang
Last synced: 5 months ago
JSON representation
Functional tools in Go 1.18 using newly introduced generics
- Host: GitHub
- URL: https://github.com/rakeeb-hossain/functools
- Owner: rakeeb-hossain
- License: mit
- Created: 2021-12-15T21:39:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-09-23T02:31:00.000Z (almost 4 years ago)
- Last Synced: 2024-06-18T23:12:44.630Z (about 2 years ago)
- Topics: functional, generics, go, golang
- Language: Go
- Homepage:
- Size: 51.8 KB
- Stars: 176
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# functools
[](https://github.com/rakeeb-hossain/functools/actions/workflows/go.yml)
functools is a simple Go library that brings you your favourite functional paradigms without sacrificing type-safety using
`interface{}` or `reflect`
Made possible by Go 1.18 using the newly introduced generics.
## Features
- Any
- All
- Count
- Filter
- ForEach
- Map
- Reduce
- ReduceRight
- Sum
- Chunk
## Installation
`go get -u github.com/rakeeb-hossain/functools`
## Usage
```go
import (
"github.com/rakeeb-hossain/functools"
"fmt"
)
type User struct {
username string
hasPortfolio bool
}
var users = []User{
{"gopher", true},
{"rakeeb", false},
{"jack", true}}
func main() {
// Count users with linked portfolios
fmt.Printf("num users with linked portfolios: %d",
functools.Count(users, func(u User) bool { return u.hasPortfolio }))
// Print usernames of users with linked portfolios
functools.ForEach(
functools.Filter(users, func(u User) bool { return u.hasPortfolio }),
func(u User) { fmt.Printf("%s has a linked portfolio\n", u.username) })
}
```
## Documentation
https://pkg.go.dev does not yet support Go 1.18 packages that use generics: https://github.com/golang/go/issues/48264
For now, documentation is provided via comments and by running `go doc -all` from the package directory.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md)