A lightweight Go library for functional-style operations on slices and maps, inspired by Java Streams.
- Chainable operations: Filter, map, sort, and more in a fluent style
- Type-safe: Uses Go generics for type safety
- Lazy evaluation: Operations are applied sequentially
- Immutable: Each operation returns a new stream
go get github.com/Meng-Xin/go-streampackage main
import go_stream "github.com/Meng-Xin/go-stream"
func main(){
s := []int{1, 2, 3}
stream := go_stream.OfSlices(s)
}result := go_stream.OfSlices([]int{1, 2, 3, 4}).
Filter(func(x int) bool { return x%2 == 0 }).
Collect() // [2, 4]result := go_stream.OfSlices([]int{1, 2, 3}).
Map(func(x int) int { return x * 2 }).
Collect() // [2, 4, 6]result := go_stream.OfSlices([]int{3, 1, 2}).
Sorted(func(a, b int) bool { return a < b }).
Collect() // [1, 2, 3]result := go_stream.OfSlices([]int{1, 2, 3, 4}).
Filter(func(x int) bool { return x%2 == 0 }).
Map(func(x int) int { return x * 2 }).
Collect() // [4, 8]m := map[string]int{"a": 1, "b": 2}
stream := go_stream.OfMaps(m)result := go_stream.OfMaps(map[string]int{"a": 1, "b": 2, "c": 3}).
Filter(func(x int) bool { return x%2 != 0 }).
Collect() // map[string]int{"a": 1, "c": 3}result := go_stream.OfMaps(map[string]int{"a": 1, "b": 2}).
Map(func(x int) int { return x * 2 }).
Collect() // map[string]int{"a": 2, "b": 4}result := go_stream.OfMaps(map[string]int{"a": 3, "b": 1, "c": 2}).
CollectToSlice(func(a, b int) bool { return a < b }) // [1, 2, 3]Operations are optimized for performance:
- Minimal allocations
- Pre-sized collections where possible
- Efficient iteration
See the test files for more complete examples:
slices_test.gomaps_test.go
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.