Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nilsmagnus/functions
Handy collection functions for the go language
https://github.com/nilsmagnus/functions
generics go golang
Last synced: 11 days ago
JSON representation
Handy collection functions for the go language
- Host: GitHub
- URL: https://github.com/nilsmagnus/functions
- Owner: nilsmagnus
- License: other
- Created: 2021-12-22T10:13:49.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-28T08:04:26.000Z (7 months ago)
- Last Synced: 2025-01-06T02:10:07.794Z (19 days ago)
- Topics: generics, go, golang
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
![tests passing](https://github.com/nilsmagnus/functions/actions/workflows/go.yml/badge.svg)
# functions to go
Handy collection-functions for the `go` language.
Functions to support common collection-operations:
* `map`
* `reduce`
* `filter`
* `associate`,`associateby`
* `contains`, `containsAll`
* `first`, `last`
* `forEach`, `forEachIndexed`
* `distinct`Coming soon: See godoc for complete documentation.
## getting started
go get github.com/nilsmagnus/functions
```go
package mainimport (
"fmt"
"github.com/nilsmagnus/functions"
"log"
)func main() {
items := []int{1, 2, 99}mappedItems := functions.Map(items, func(a int) string { return fmt.Sprintf("mapped %d", a) })
log.Printf("Mapped items: %v", mappedItems)
}```
Run this example on the [playground](https://go.dev/play/p/tpcenABL62q?v=gotip)
Have a look at [functions_test.go](functions_test.go) for usage.
### Map-function
```gonumbers := []int{1,2,3,199}
mapd := Map(numbers, func(a int) string { return fmt.Sprintf("%d", a)})
// mapd => []string{"1", "2", "3", "199"}
```### AssociateBy-function
```go
numbers := []int{1,2,3,199}mapd := AssociateBy(numbers, func(t int) string {
return fmt.Sprintf("%d", t)
})// => map[string]int {"1":1, "2":2, "3":3, "199": 199}
```