https://github.com/chinalhr/go-stream
Use Go to implement Java Stream API
https://github.com/chinalhr/go-stream
golang library stream stream-processing
Last synced: 4 months ago
JSON representation
Use Go to implement Java Stream API
- Host: GitHub
- URL: https://github.com/chinalhr/go-stream
- Owner: ChinaLHR
- License: mit
- Created: 2022-05-16T15:36:35.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-03-09T03:18:15.000Z (over 3 years ago)
- Last Synced: 2024-12-30T14:52:28.393Z (over 1 year ago)
- Topics: golang, library, stream, stream-processing
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## go-stream
[](https://golang.org/)
[](https://img.shields.io/github/go-mod/go-version/chinalhr/go-stream)
[](https://github.com/chinalhr/go-stream)
[](https://github.com/ChinaLHR/go-stream/actions/workflows/build.yaml)
[](https://goreportcard.com/report/github.com/chinalhr/go-stream)
[](https://codecov.io/gh/chinalhr/go-stream)
[](https://github.com/chinalhr/go-stream/blob/main/LICENSE)
## Overview
Go-Stream is a stream processing library to implement the Java Stream API with Go.
## Features
- `non-storage`: Stream is not a data structure, but the view of the data source. The data source can come from slice, map or supplier function.
- `pipeline`: Operate the elements through pipeline, support short-circuit and parallel execution.
- `lazy-evaluation`: The intermediate operation on the stream is lazy, and it will be truly executed only when the terminal operation is performed.
Go-Stream supports the following operations
| Stream Operation | | |
| --------------------------- | -------------------- | ------------------------------------------------------------ |
| **Intermediate operations** | Stateless | Filter、Map、Peek、FlatMap |
| | Stateful | Distinct、Sorted、Skip、Limit、TakeWhile、DropWhile |
| **Terminal operations** | non short-circuiting | ForEach、Reduce、ReduceFromIdentity、Count、Max、Min、FindLast、ToSlice、ToMap、GroupingBy |
| | short-circuiting | AllMatch、AnyMatch、NoneMatch、FindFirst |
## Quick Start
1. installation go-stream library
```
go get github.com/chinalhr/go-stream
```
2. import
```
import "github.com/chinalhr/go-stream"
```
3. Use Stream to process data
```go
type widget struct {
color string
weight int
}
widgets := []widget{
{
color: "yellow",
weight: 4,
},
{
color: "red",
weight: 3,
},
{
color: "yellow",
weight: 2,
},
{
color: "blue",
weight: 1,
},
}
sum := stream.OfSlice(widgets).
Filter(func(e types.T) bool {
return e.(widget).color == "yellow"
}).
Map(func(e types.T) (r types.R) {
return e.(widget).weight
}).
Reduce(func(e1 types.T, e2 types.T) types.T {
return e1.(int) + e2.(int)
})
```