Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samwho/fu
A collection of functional utilities written in Golang.
https://github.com/samwho/fu
Last synced: about 2 months ago
JSON representation
A collection of functional utilities written in Golang.
- Host: GitHub
- URL: https://github.com/samwho/fu
- Owner: samwho
- Created: 2019-05-18T12:30:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-05-26T15:34:48.000Z (over 5 years ago)
- Last Synced: 2024-10-14T20:39:51.737Z (3 months ago)
- Language: Go
- Size: 35.2 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fu: functional utilities
If you miss your maps, reducers and filters from other languages, and don't mind manually unwrapping `interface{}`s, this library is for you.
## Mapping
```go
ctx := context.Background()
ns := []interface{}{1, 2, 3, 4, 5}
ms, err := fu.MapFn(ctx, ns, func(ctx context.Context, i interface{}) (interface{}, error){
n, ok := i.(int)
if !ok {
return errors.New("invalid type")
}
return n + 1
})
if err != nil {
return err
}
fmt.Printf("%v\n", ms) // []interface{}{2, 3, 4, 5, 6}
```Or as a shorthand:
```go
ctx := context.Background()
ns := []interface{}{1, 2, 3, 4, 5}
ms, err := fu.Map(ctx, ns, fu.Add(1))
if err != nil {
return err
}
fmt.Printf("%v\n", ms) // []interface{}{2, 3, 4, 5, 6}
```And if you want to do stuff in parallel:
```go
ctx := context.Background()
ns := []interface{}{1, 2, 3, 4, 5}
ms, err := fu.ParallelMap(ctx, ns, fu.Add(1))
if err != nil {
return err
}
fmt.Printf("%v\n", ms) // []interface{}{2, 3, 4, 5, 6}
```