1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-23 13:52:26 +08:00

Compare commits

...

3 Commits

Author SHA1 Message Date
dudaodong
82cbb54787 feat: add FromChannel for create stream 2023-01-17 16:47:20 +08:00
dudaodong
585d33cafa feat: add Generate for create stream 2023-01-17 16:31:44 +08:00
dudaodong
bc4cf35e15 feat: add Filter, Map, Count for stream 2023-01-17 14:59:47 +08:00
2 changed files with 132 additions and 2 deletions

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by MIT license
// Package stream implements a sequence of elements supporting sequential and parallel aggregate operations.
// this package is a experiment to explore if stream in go can work as the way java does. it's complete, but not
// this package is an experiment to explore if stream in go can work as the way java does. it's complete, but not
// powerful like other libs
package stream
@@ -51,11 +51,43 @@ type stream[T any] struct {
source []T
}
// Of creates a stream stream whose elements are the specified values.
func Of[T any](elems ...T) stream[T] {
return FromSlice(elems)
}
// Generate stream where each element is generated by the provided generater function
// generater function: func() func() (item T, ok bool) {}
func Generate[T any](generator func() func() (item T, ok bool)) stream[T] {
source := make([]T, 0)
var zeroValue T
for next, item, ok := generator(), zeroValue, true; ok; {
item, ok = next()
if ok {
source = append(source, item)
}
}
return FromSlice(source)
}
// FromSlice create stream from slice.
func FromSlice[T any](source []T) stream[T] {
return stream[T]{source: source}
}
// FromChannel create stream from channel.
func FromChannel[T any](source <-chan T) stream[T] {
s := make([]T, 0)
for v := range source {
s = append(s, v)
}
return FromSlice(s)
}
// FromRange create a number stream from start to end. both start and end are included. [start, end]
func FromRange[T constraints.Integer | constraints.Float](start, end, step T) stream[T] {
if end < start {
@@ -71,7 +103,7 @@ func FromRange[T constraints.Integer | constraints.Float](start, end, step T) st
source[i] = start + (T(i) * step)
}
return stream[T]{source: source}
return FromSlice(source)
}
// Distinct returns a stream that removes the duplicated items.
@@ -102,6 +134,35 @@ func hashKey(data any) string {
return buffer.String()
}
// Filter returns a stream consisting of the elements of this stream that match the given predicate.
func (s stream[T]) Filter(predicate func(item T) bool) stream[T] {
source := make([]T, 0)
for _, v := range s.source {
if predicate(v) {
source = append(source, v)
}
}
return FromSlice(source)
}
// Map returns a stream consisting of the elements of this stream that apply the given function to elements of stream.
func (s stream[T]) Map(mapper func(item T) T) stream[T] {
source := make([]T, s.Count(), s.Count())
for i, v := range s.source {
source[i] = mapper(v)
}
return FromSlice(source)
}
// Count returns the count of elements in the stream.
func (s stream[T]) Count() int {
return len(s.source)
}
// ToSlice return the elements in the stream.
func (s stream[T]) ToSlice() []T {
return s.source

View File

@@ -6,6 +6,31 @@ import (
"github.com/duke-git/lancet/v2/internal"
)
func TestOf(t *testing.T) {
assert := internal.NewAssert(t, "TestFromSlice")
stream := Of(1, 2, 3)
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestGenerate(t *testing.T) {
assert := internal.NewAssert(t, "TestFromSlice")
n := 0
max := 4
generator := func() func() (int, bool) {
return func() (int, bool) {
n++
return n, n < max
}
}
stream := Generate(generator)
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestFromSlice(t *testing.T) {
assert := internal.NewAssert(t, "TestFromSlice")
@@ -14,6 +39,22 @@ func TestFromSlice(t *testing.T) {
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestFromChannel(t *testing.T) {
assert := internal.NewAssert(t, "TestFromChannel")
ch := make(chan int)
go func() {
for i := 1; i < 4; i++ {
ch <- i
}
close(ch)
}()
stream := FromChannel(ch)
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestFromRange(t *testing.T) {
assert := internal.NewAssert(t, "TestFromRange")
@@ -55,3 +96,31 @@ func TestStream_Distinct(t *testing.T) {
// {[{001 Tom 10} {002 Jim 20} {003 Mike 30}]}
t.Log(distinctStream)
}
func TestStream_Filter(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Filter")
stream := FromSlice([]int{1, 2, 3, 4, 5})
isEven := func(n int) bool {
return n%2 == 0
}
even := stream.Filter(isEven)
assert.Equal([]int{2, 4}, even.ToSlice())
}
func TestStream_Map(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Map")
stream := FromSlice([]int{1, 2, 3})
addOne := func(n int) int {
return n + 1
}
s := stream.Map(addOne)
assert.Equal([]int{2, 3, 4}, s.ToSlice())
}