Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dabbertorres/stream
streams package for Go
https://github.com/dabbertorres/stream
Last synced: about 2 months ago
JSON representation
streams package for Go
- Host: GitHub
- URL: https://github.com/dabbertorres/stream
- Owner: dabbertorres
- License: mit
- Created: 2023-01-17T23:52:06.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-29T19:27:03.000Z (9 months ago)
- Last Synced: 2024-06-20T05:14:11.401Z (7 months ago)
- Language: Go
- Size: 44.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# stream
streams package for Go.
[docs](https://pkg.go.dev/github.com/dabbertorres/stream)
## Quick Examples
### Stream a slice
```go
func ExampleFromSlice() {
var (
filterCalled bool
transformCalled bool
)stream := FromSlice([]int{3, 8, 11, 24, 37, 42}).
Filter(func(elem int) bool { filterCalled = true; return elem >= 10 }).
Skip(1).
Limit(2).
Transform(func(elem int) int { transformCalled = true; return elem * 2 })fmt.Println(filterCalled) // NOTE: false is printed here - streams are lazily evaluated
fmt.Println(transformCalled) // NOTE: false is printed here - streams are lazily evaluated
fmt.Println(stream.First().Get()) // NOTE: the stream has now been consumed
fmt.Println(stream.FirstWhere(func(i int) bool { return i%2 == 1 }).Get())
fmt.Println(filterCalled)
fmt.Println(transformCalled)// Output:
// false
// false
// 48 true
// 0 false
// true
// true
}
```### Stream a channel
```go
func ExampleFromChan() {
var (
filterCalled bool
transformCalled bool
)ch := make(chan int)
go func() {
defer close(ch)
ch <- 3
ch <- 8
ch <- 11
ch <- 24
ch <- 37
ch <- 42
}()stream := FromChan(ch).
Filter(func(elem int) bool { filterCalled = true; return elem >= 10 }).
Skip(1).
Limit(2).
Transform(func(elem int) int { transformCalled = true; return elem * 2 })fmt.Println(filterCalled) // NOTE: false is printed here - streams are lazily evaluated
fmt.Println(transformCalled) // NOTE: false is printed here - streams are lazily evaluated
fmt.Println(stream.First().Get())
fmt.Println(stream.FirstWhere(func(i int) bool { return i%2 == 1 }).Get())
fmt.Println(stream.First().Get())
fmt.Println(filterCalled)
fmt.Println(transformCalled)// Output:
// false
// false
// 48 true
// 0 false
// 0 false
// true
// true
}
```