mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
2 Commits
bc3c080ac3
...
a3bc20af1d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a3bc20af1d | ||
|
|
61338b6b46 |
108
stream/stream.go
Normal file
108
stream/stream.go
Normal file
@@ -0,0 +1,108 @@
|
||||
// Copyright 2023 dudaodong@gmail.com. All rights resulterved.
|
||||
// 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
|
||||
// powerful like other libs
|
||||
package stream
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
// A stream should implements methods:
|
||||
// type StreamI[T any] interface {
|
||||
|
||||
// // part methods of Java Stream Specification.
|
||||
// Distinct() StreamI[T]
|
||||
// Filter(predicate func(item T) bool) StreamI[T]
|
||||
// FlatMap(mapper func(item T) StreamI[T]) StreamI[T]
|
||||
// Map(mapper func(item T) T) StreamI[T]
|
||||
// Peek(consumer func(item T)) StreamI[T]
|
||||
|
||||
// Sort(less func(a, b T) bool) StreamI[T]
|
||||
// Max(less func(a, b T) bool) (T, bool)
|
||||
// Min(less func(a, b T) bool) (T, bool)
|
||||
|
||||
// Limit(maxSize int) StreamI[T]
|
||||
// Skip(n int64) StreamI[T]
|
||||
|
||||
// AllMatch(predicate func(item T) bool) bool
|
||||
// AnyMatch(predicate func(item T) bool) bool
|
||||
// NoneMatch(predicate func(item T) bool) bool
|
||||
// ForEach(consumer func(item T))
|
||||
// Reduce(accumulator func(a, b T) T) (T, bool)
|
||||
// Count() int
|
||||
|
||||
// FindFirst() (T, bool)
|
||||
|
||||
// ToSlice() []T
|
||||
|
||||
// // part of methods custom extension
|
||||
// Reverse() StreamI[T]
|
||||
// Range(start, end int64) StreamI[T]
|
||||
// Concat(streams ...StreamI[T]) StreamI[T]
|
||||
// }
|
||||
|
||||
type stream[T any] struct {
|
||||
source []T
|
||||
}
|
||||
|
||||
// FromSlice create stream from slice.
|
||||
func FromSlice[T any](source []T) stream[T] {
|
||||
return stream[T]{source: source}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
panic("stream.FromRange: param start should be before param end")
|
||||
} else if step <= 0 {
|
||||
panic("stream.FromRange: param step should be positive")
|
||||
}
|
||||
|
||||
l := int((end-start)/step) + 1
|
||||
source := make([]T, l, l)
|
||||
|
||||
for i := 0; i < l; i++ {
|
||||
source[i] = start + (T(i) * step)
|
||||
}
|
||||
|
||||
return stream[T]{source: source}
|
||||
}
|
||||
|
||||
// Distinct returns a stream that removes the duplicated items.
|
||||
func (s stream[T]) Distinct() stream[T] {
|
||||
source := make([]T, 0)
|
||||
|
||||
distinct := map[string]bool{}
|
||||
|
||||
for _, v := range s.source {
|
||||
// todo: performance issue
|
||||
k := hashKey(v)
|
||||
if _, ok := distinct[k]; !ok {
|
||||
distinct[k] = true
|
||||
source = append(source, v)
|
||||
}
|
||||
}
|
||||
|
||||
return FromSlice(source)
|
||||
}
|
||||
|
||||
func hashKey(data any) string {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
encoder := gob.NewEncoder(buffer)
|
||||
err := encoder.Encode(data)
|
||||
if err != nil {
|
||||
panic("stream.hashKey: get hashkey failed")
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// ToSlice return the elements in the stream.
|
||||
func (s stream[T]) ToSlice() []T {
|
||||
return s.source
|
||||
}
|
||||
57
stream/stream_test.go
Normal file
57
stream/stream_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/v2/internal"
|
||||
)
|
||||
|
||||
func TestFromSlice(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestFromSlice")
|
||||
|
||||
stream := FromSlice([]int{1, 2, 3})
|
||||
|
||||
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
|
||||
}
|
||||
|
||||
func TestFromRange(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestFromRange")
|
||||
|
||||
s1 := FromRange(1, 5, 1)
|
||||
s2 := FromRange(1.1, 5.0, 1.0)
|
||||
|
||||
assert.Equal([]int{1, 2, 3, 4, 5}, s1.ToSlice())
|
||||
assert.Equal([]float64{1.1, 2.1, 3.1, 4.1}, s2.ToSlice())
|
||||
}
|
||||
|
||||
func TestStream_Distinct(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestStream_Distinct")
|
||||
|
||||
nums := FromSlice([]int{1, 2, 2, 3, 3, 3})
|
||||
distinctNums := nums.Distinct()
|
||||
|
||||
assert.Equal([]int{1, 2, 2, 3, 3, 3}, nums.ToSlice())
|
||||
assert.Equal([]int{1, 2, 3}, distinctNums.ToSlice())
|
||||
|
||||
type Person struct {
|
||||
Id string
|
||||
Name string
|
||||
Age uint
|
||||
}
|
||||
|
||||
people := []Person{
|
||||
{Id: "001", Name: "Tom", Age: 10},
|
||||
{Id: "001", Name: "Tom", Age: 10},
|
||||
{Id: "002", Name: "Jim", Age: 20},
|
||||
{Id: "003", Name: "Mike", Age: 30},
|
||||
}
|
||||
|
||||
stream := FromSlice(people)
|
||||
distinctStream := stream.Distinct()
|
||||
|
||||
// {[{001 Tom 10} {001 Tom 10} {002 Jim 20} {003 Mike 30}]}
|
||||
t.Log(stream)
|
||||
|
||||
// {[{001 Tom 10} {002 Jim 20} {003 Mike 30}]}
|
||||
t.Log(distinctStream)
|
||||
}
|
||||
Reference in New Issue
Block a user