Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iancanderson/cowabunga
Handy slice functions using go generics
https://github.com/iancanderson/cowabunga
go
Last synced: 6 days ago
JSON representation
Handy slice functions using go generics
- Host: GitHub
- URL: https://github.com/iancanderson/cowabunga
- Owner: iancanderson
- License: mit
- Created: 2022-01-22T19:12:07.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-24T02:56:46.000Z (almost 3 years ago)
- Last Synced: 2024-12-17T02:36:01.823Z (6 days ago)
- Topics: go
- Language: Go
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cowabunga
Handy slice 🍕 functions using go generics
## Usage
```go
import "github.com/iancanderson/cowabunga"ints := []int{1, 2, 3}
allAreBig := All(ints, func(n int) bool { return n > 2 })
// falseanyAreBig := Any(ints, func(n int) bool { return n > 2 })
// truenumBig := Count(ints, func(n int) bool { return n > 2 })
// 1lastTwo := Drop(ints, 1)
// [2, 3]lastOne := DropWhile(ints, func(n int) bool { return n < 3 })
// [3]EachCons(ints, 2, func(nums []int) {
fmt.Println(nums)
})
// [1, 2]
// [2, 3]EachSlice(ints, 2, func(nums []int) {
fmt.Println(nums)
})
// [1, 2]
// [3]sum := 0
sum = EachWith(ints, &sum, func(num int, result *int) {
*result += n
})
// 6bigNumbers := Filter(ints, func(n int) bool { return n > 2 })
// [3]bigNumbersIncremented := FilterMap(ints, func(n int) *string {
if n > 2 {
str := strconv.Itoa(n + 1)
return &str
} else {
return nil
}
})
// ["4"]firstGreaterThanOne := Find(ints, func(n int) bool { return n > 1 })
// [&2]firstNum := First(ints)
// 1strings := FlatMap(ints, func(n int) string {
return []string{strconv.Itoa(n), strconv.Itoa(n + 1)}
})
// ["1", "2", "2", "3", "3", "4"]byEven := GroupBy(ints, func(n int) bool { return n % 2 == 0 })
// map[true:[]int{2} false:[]int{1, 3}]hasATwo := IsMember(ints, 2)
// truelastNum := Last(ints)
// 3strings := Map(ints, func(n int) string { return strconv.Itoa(n + 1) })
// ["2", "3", "4"]firstTwo := Take(ints, 2)
// [1, 2]
```