https://github.com/berquerant/circle
stream library for Go
https://github.com/berquerant/circle
go
Last synced: about 1 year ago
JSON representation
stream library for Go
- Host: GitHub
- URL: https://github.com/berquerant/circle
- Owner: berquerant
- License: mit
- Created: 2020-07-15T18:29:46.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-07-18T05:55:26.000Z (almost 4 years ago)
- Last Synced: 2025-02-06T16:57:58.785Z (over 1 year ago)
- Topics: go
- Language: Go
- Homepage:
- Size: 120 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## circle
circle provides sequences which support aggregation operations.
[](https://godoc.org/github.com/berquerant/circle)
### Example
``` go
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/berquerant/circle"
)
// tr -d ' ' | tr '[:upper:]' '[:lower:]' | grep -o . | sort | uniq -c | sort -rnk 1 | awk '{print $2, $1}'
func main() {
sc := bufio.NewScanner(os.Stdin)
_ = circle.NewStreamBuilder(circle.MustNewIterator(func() (interface{}, error) {
if sc.Scan() {
return sc.Text(), nil
}
if err := sc.Err(); err != nil {
return nil, err
}
return nil, circle.ErrEOI
})).
Filter(func(x string) bool { return x != "" }).
Map(func(x string) string { return strings.ReplaceAll(x, " ", "") }).
Map(strings.ToLower).
Map(func(x string) []string { return strings.Split(x, "") }).
Flat().
Aggregate(func(d map[string]int, x string) map[string]int {
d[x]++
return d
}, map[string]int{}).
Flat().
Sort(func(x, y circle.Tuple) bool { return x.MustGet(1).(int) > y.MustGet(1).(int) }).
TupleMap(func(x string, y int) string { return fmt.Sprintf("%s %d", x, y) }).
Consume(func(x string) { fmt.Println(x) })
}
```
### Test
``` shell
make test
```