Documentation
ΒΆ
Overview ΒΆ
Package stream implements like Java 8 Streams for Go. Use channel and Go1.18 generic support.
Index ΒΆ
- func Copy[T any](source *BaseStream[T]) chan T
- func Count[T any](input <-chan T) int
- func Distinct[C comparable](input <-chan C) chan C
- func Distribute[T any](input <-chan T, output1, output2 chan<- T)
- func Filter[T any](input <-chan T, action FilterAction[T]) chan T
- func FilterParallel[T any](input <-chan T, size int, action FilterAction[T]) chan T
- func Limit[T any](input <-chan T, maxSize int) chan T
- func Peek[T any](input <-chan T, action Action[T]) chan T
- func PeekParallel[T any](input <-chan T, size int, action Action[T]) chan T
- func Range2Channel(length int) chan int
- func RangeFrom2Channel(start, length int) chan int
- func RangeWithSteps2Channel(start, end, step int) chan int
- func Reduce[O constraints.Ordered](input <-chan O) chan O
- func Skip[T any](input <-chan T, size int) chan T
- func Slice2Channel[T any](elems ...T) chan T
- func Sort[T any](input <-chan T, action LessAction[T]) chan T
- func Sorted[O constraints.Ordered](input <-chan O) chan O
- type Action
- type BaseStream
- func (p *BaseStream[T]) AllMatch(action FilterAction[T]) bool
- func (p *BaseStream[T]) AnyMatch(action FilterAction[T]) bool
- func (p *BaseStream[T]) Count() int
- func (p *BaseStream[T]) Filter(action FilterAction[T]) *BaseStream[T]
- func (p *BaseStream[T]) FindFirst() T
- func (p *BaseStream[T]) ForEach(action Action[T])
- func (p *BaseStream[T]) Limit(maxSize int) *BaseStream[T]
- func (p *BaseStream[T]) NoneMatch(action FilterAction[T]) bool
- func (p *BaseStream[T]) Parallel(size int) *ParallelStream[T]
- func (p *BaseStream[T]) Peek(action Action[T]) *BaseStream[T]
- func (p *BaseStream[T]) Skip(size int) *BaseStream[T]
- func (p *BaseStream[T]) Sort(action LessAction[T]) *BaseStream[T]
- type ComparableStream
- type FilterAction
- type LessAction
- type MapAction
- type OrderedStream
- type ParallelStream
Examples ΒΆ
- BaseStream.AllMatch
- BaseStream.AnyMatch
- BaseStream.Filter
- BaseStream.FindFirst
- BaseStream.ForEach
- BaseStream.Limit
- BaseStream.NoneMatch
- BaseStream.Peek
- BaseStream.Skip
- BaseStream.Sort
- ComparableStream.Distinct
- Filter
- FlatMap
- FlatMapComparable
- FlatMapOrdered
- Map
- MapComparable
- MapOrdered
- NewBase
- OrderedStream.Min
- OrderedStream.Reduce
- OrderedStream.Sorted
- ParallelStream.Filter
- ParallelStream.ForEach
- ParallelStream.Peek
- ParallelStream.Sequential
- Range2Channel
- RangeFrom2Channel
- RangeWithSteps2Channel
- Slice2Channel
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func Copy ΒΆ
func Copy[T any](source *BaseStream[T]) chan T
func Distinct ΒΆ
func Distinct[C comparable](input <-chan C) chan C
Distinct returns a channel consisting of the distinct elements of input channel.
func Distribute ΒΆ
func Distribute[T any](input <-chan T, output1, output2 chan<- T)
func Filter ΒΆ
func Filter[T any](input <-chan T, action FilterAction[T]) chan T
Filter returns a channel consisting of the elements of input channel that match the given action.
Example ΒΆ
chi := stream.Filter(
stream.Range2Channel(10),
func(num int) bool { return num > 5 },
)
for i := range chi {
fmt.Println(i)
}
Output: 6 7 8 9
func FilterParallel ΒΆ
func FilterParallel[T any](input <-chan T, size int, action FilterAction[T]) chan T
FilterParallel returns a channel consisting of the elements of input channel that match the given action, parallel.
func Limit ΒΆ
Limit returns a channel consisting of the elements of input channel, truncated to be no longer than maxSize in length.
func Peek ΒΆ
Peek returns a channel consisting of the elements of input channel, additionally performing the provided action on each element as elements are consumed from the resulting stream.
func PeekParallel ΒΆ
func Range2Channel ΒΆ
Example ΒΆ
for i := range stream.Range2Channel(3) {
fmt.Println(i)
}
for i := range stream.Range2Channel(-3) {
fmt.Println(i)
}
Output: 0 1 2 0 -1 -2
func RangeFrom2Channel ΒΆ
Example ΒΆ
for i := range stream.RangeFrom2Channel(3, 2) {
fmt.Println(i)
}
for i := range stream.RangeFrom2Channel(8, -3) {
fmt.Println(i)
}
Output: 3 4 8 7 6
func RangeWithSteps2Channel ΒΆ
Example ΒΆ
for i := range stream.RangeWithSteps2Channel(0, 4, 3) {
fmt.Println(i)
}
for i := range stream.RangeWithSteps2Channel(9, 5, -3) {
fmt.Println(i)
}
Output: 0 3 9 6
func Reduce ΒΆ
func Reduce[O constraints.Ordered](input <-chan O) chan O
func Slice2Channel ΒΆ
func Slice2Channel[T any](elems ...T) chan T
Example ΒΆ
for i := range stream.Slice2Channel(1, 2, 3) {
fmt.Println(i)
}
Output: 1 2 3
func Sort ΒΆ
func Sort[T any](input <-chan T, action LessAction[T]) chan T
func Sorted ΒΆ
func Sorted[O constraints.Ordered](input <-chan O) chan O
Types ΒΆ
type BaseStream ΒΆ
type BaseStream[T any] struct { C chan T }
func FlatMap ΒΆ
func FlatMap[I, O any](input chan []I, action MapAction[I, O]) *BaseStream[O]
Example ΒΆ
stream.FlatMap(
stream.Slice2Channel([]int{0, 0}, []int{1, 2}, []int{2, 4}),
func(num int) string { return fmt.Sprintf("[%d]", num) },
).ForEach(func(str string) {
fmt.Println(str)
})
Output: [0] [0] [1] [2] [2] [4]
func Map ΒΆ
func Map[I, O any](input chan I, action MapAction[I, O]) *BaseStream[O]
Example ΒΆ
base1 := stream.NewBase(stream.Range2Channel(10)).
Filter(func(num int) bool { return num > 5 })
base2 := stream.Map(
base1.C,
func(num int) string { return fmt.Sprintf("[%d]", num) },
)
for num := range base2.C {
fmt.Println(num)
}
Output: [6] [7] [8] [9]
func NewBase ΒΆ
func NewBase[T any](input chan T) *BaseStream[T]
Example ΒΆ
base := stream.NewBase(stream.Range2Channel(10)).
Filter(func(num int) bool { return num > 5 })
fmt.Println(base.Count())
Output: 4
func (*BaseStream[T]) AllMatch ΒΆ
func (p *BaseStream[T]) AllMatch(action FilterAction[T]) bool
Example ΒΆ
base1 := stream.NewBase(stream.Range2Channel(10))
base2 := stream.NewBase(stream.Copy(base1))
fmt.Println(base1.AllMatch(func(num int) bool { return num > 8 }))
fmt.Println(base2.AllMatch(func(num int) bool { return num >= 0 }))
Output: false true
func (*BaseStream[T]) AnyMatch ΒΆ
func (p *BaseStream[T]) AnyMatch(action FilterAction[T]) bool
Example ΒΆ
base1 := stream.NewBase(stream.Range2Channel(10))
base2 := stream.NewBase(stream.Copy(base1))
fmt.Println(base1.AnyMatch(func(num int) bool { return num > 100 }))
fmt.Println(base2.AnyMatch(func(num int) bool { return num > 1 }))
Output: false true
func (*BaseStream[T]) Count ΒΆ
func (p *BaseStream[T]) Count() int
func (*BaseStream[T]) Filter ΒΆ
func (p *BaseStream[T]) Filter(action FilterAction[T]) *BaseStream[T]
Filter returns a stream consisting of the elements of this stream that match the given action.
Example ΒΆ
stream.NewBase(stream.Range2Channel(10)).
Filter(func(num int) bool { return num > 5 }).
ForEach(func(num int) {
fmt.Println(num)
})
Output: 6 7 8 9
func (*BaseStream[T]) FindFirst ΒΆ
func (p *BaseStream[T]) FindFirst() T
Example ΒΆ
base := stream.NewBase(stream.Range2Channel(10)).
Filter(func(num int) bool { return num > 5 })
fmt.Println(base.FindFirst())
Output: 6
func (*BaseStream[T]) ForEach ΒΆ
func (p *BaseStream[T]) ForEach(action Action[T])
Example ΒΆ
stream.NewBase(stream.Range2Channel(10)).
ForEach(func(num int) { fmt.Println(num) })
Output: 0 1 2 3 4 5 6 7 8 9
func (*BaseStream[T]) Limit ΒΆ
func (p *BaseStream[T]) Limit(maxSize int) *BaseStream[T]
Limit returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
Example ΒΆ
stream.NewBase(stream.Range2Channel(10)).
Limit(3).
ForEach(func(num int) {
fmt.Println(num)
})
Output: 0 1 2
func (*BaseStream[T]) NoneMatch ΒΆ
func (p *BaseStream[T]) NoneMatch(action FilterAction[T]) bool
Example ΒΆ
base1 := stream.NewBase(stream.Range2Channel(10))
base2 := stream.NewBase(stream.Copy(base1))
fmt.Println(base1.NoneMatch(func(num int) bool { return num > 100 }))
fmt.Println(base2.NoneMatch(func(num int) bool { return num > 1 }))
Output: true false
func (*BaseStream[T]) Parallel ΒΆ
func (p *BaseStream[T]) Parallel(size int) *ParallelStream[T]
func (*BaseStream[T]) Peek ΒΆ
func (p *BaseStream[T]) Peek(action Action[T]) *BaseStream[T]
Peek returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
Example ΒΆ
count1 := 0
count2 := 0
base := stream.NewBase(stream.Range2Channel(3)).
Peek(func(num int) { count1++ }).
Filter(func(num int) bool { return num%2 == 0 }).
Peek(func(num int) { count2++ })
fmt.Println(base.Count())
fmt.Println(count1)
fmt.Println(count2)
Output: 2 3 2
func (*BaseStream[T]) Skip ΒΆ
func (p *BaseStream[T]) Skip(size int) *BaseStream[T]
Example ΒΆ
stream.NewBase(stream.Range2Channel(6)).
Skip(4).
ForEach(func(num int) {
fmt.Println(num)
})
Output: 4 5
func (*BaseStream[T]) Sort ΒΆ
func (p *BaseStream[T]) Sort(action LessAction[T]) *BaseStream[T]
Example ΒΆ
stream.NewBase(stream.Slice2Channel(3, 2, 7, 1)).
Sort(func(num1, num2 int) bool { return num2 < num1 }).
ForEach(func(num int) {
fmt.Println(num)
})
Output: 7 3 2 1
type ComparableStream ΒΆ
type ComparableStream[C comparable] struct { BaseStream[C] }
func FlatMapComparable ΒΆ
func FlatMapComparable[I any, O comparable](input chan []I, action MapAction[I, O]) *ComparableStream[O]
Example ΒΆ
stream.FlatMapOrdered(
stream.Slice2Channel([]int{1, 2}, []int{2, 4}, []int{3, 6}),
func(num int) string { return fmt.Sprintf("[%d]", num) },
).
Sorted().
ForEach(func(str string) {
fmt.Println(str)
})
Output: [1] [2] [2] [3] [4] [6]
func MapComparable ΒΆ
func MapComparable[I any, O comparable](input chan I, action MapAction[I, O]) *ComparableStream[O]
Example ΒΆ
com := stream.MapComparable(
stream.Slice2Channel(1, 1, 1, 2, 3, 3, 4),
func(num int) string { return fmt.Sprintf("[%d]", num) },
).Distinct()
for i := range com.C {
fmt.Println(i)
}
Output: [1] [2] [3] [4]
func NewComparable ΒΆ
func NewComparable[C comparable](input chan C) *ComparableStream[C]
func (*ComparableStream[C]) Distinct ΒΆ
func (p *ComparableStream[C]) Distinct() *ComparableStream[C]
Distinct returns a stream consisting of the distinct elements of this stream.
Example ΒΆ
stream.NewComparable(stream.Slice2Channel(1, 1, 2, 3, 3, 4)).
Distinct().
ForEach(func(num int) {
fmt.Println(num)
})
Output: 1 2 3 4
type FilterAction ΒΆ
type LessAction ΒΆ
type OrderedStream ΒΆ
type OrderedStream[O constraints.Ordered] struct { ComparableStream[O] }
func FlatMapOrdered ΒΆ
func FlatMapOrdered[I any, O constraints.Ordered](input chan []I, action MapAction[I, O]) *OrderedStream[O]
Example ΒΆ
stream.FlatMapComparable(stream.Slice2Channel([]int{1, 2}, []int{1, 2}, []int{2, 4},
[]int{3, 6}, []int{3, 6}, []int{4, 8}),
func(num int) string { return fmt.Sprintf("[%d]", num) },
).
Distinct().
ForEach(func(str string) {
fmt.Println(str)
})
Output: [1] [2] [4] [3] [6] [8]
func MapOrdered ΒΆ
func MapOrdered[I any, O constraints.Ordered](input chan I, action MapAction[I, O]) *OrderedStream[O]
Example ΒΆ
ordered := stream.MapOrdered(
stream.Range2Channel(10),
func(num int) string { return fmt.Sprintf("[%d]", num) },
)
fmt.Println(ordered.Max())
Output: [9]
func NewOrdered ΒΆ
func NewOrdered[O constraints.Ordered](input chan O) *OrderedStream[O]
func (*OrderedStream[O]) Max ΒΆ
func (p *OrderedStream[O]) Max() O
func (*OrderedStream[O]) Min ΒΆ
func (p *OrderedStream[O]) Min() O
Example ΒΆ
ordered := stream.MapOrdered(stream.Range2Channel(10), func(num int) string { return fmt.Sprintf("[%d]", num) })
fmt.Println(ordered.Min())
Output: [0]
func (*OrderedStream[O]) Reduce ΒΆ
func (p *OrderedStream[O]) Reduce() *OrderedStream[O]
Example ΒΆ
stream.NewOrdered(stream.Slice2Channel(3, 2, 7, 1)).
Reduce().
ForEach(func(num int) {
fmt.Println(num)
})
Output: 7 3 2 1
func (*OrderedStream[O]) Sorted ΒΆ
func (p *OrderedStream[O]) Sorted() *OrderedStream[O]
Example ΒΆ
stream.NewOrdered(stream.Slice2Channel(3, 2, 7, 1)).
Sorted().ForEach(func(num int) {
fmt.Println(num)
})
Output: 1 2 3 7
type ParallelStream ΒΆ
type ParallelStream[T any] struct { BaseStream[T] Size int }
func NewParallel ΒΆ
func NewParallel[T any](input chan T, size int) *ParallelStream[T]
func (*ParallelStream[T]) Filter ΒΆ
func (p *ParallelStream[T]) Filter(action FilterAction[T]) *ParallelStream[T]
Filter returns a stream consisting of the elements of this stream that match the given action, parallel.
Example ΒΆ
stream.NewBase(stream.Range2Channel(10)).
Parallel(3).
Filter(func(num int) bool { return num > 5 }).
ForEach(func(_ int) {
fmt.Println("x")
})
Output: x x x x
func (*ParallelStream[T]) ForEach ΒΆ
func (p *ParallelStream[T]) ForEach(action Action[T])
Example ΒΆ
stream.NewParallel(stream.Range2Channel(3), 3).
ForEach(func(num int) {
time.Sleep(time.Duration((3-num)*100) * time.Millisecond)
fmt.Println(num)
})
Output: 2 1 0
func (*ParallelStream[T]) Peek ΒΆ
func (p *ParallelStream[T]) Peek(action Action[T]) *ParallelStream[T]
Example ΒΆ
count := stream.NewParallel(stream.Range2Channel(3), 3).
Peek(func(num int) {
time.Sleep(time.Duration((3-num)*100) * time.Millisecond)
fmt.Println(num)
}).Count()
fmt.Println(count)
Output: 2 1 0 3
func (*ParallelStream[T]) Sequential ΒΆ
func (p *ParallelStream[T]) Sequential() *BaseStream[T]
Example ΒΆ
strea := stream.NewParallel(stream.Range2Channel(10), 10).
Sequential().
Filter(func(num int) bool { return num > 5 })
fmt.Println(strea.FindFirst())
Output: 6