stream
This commit is contained in:
parent
7cc974a335
commit
470312936c
@ -31,6 +31,21 @@ func SimpleParallelFilterAndMapToMap[K comparable, V any, T any](a SimpleSliceSt
|
||||
return
|
||||
}
|
||||
|
||||
func SimpleSliceFilterAndMapToMap[K comparable, V any, T any](a SimpleSliceStream[T], fn func(t T) (K, V, bool), isCoverPrev bool) (r SimpleMapStream[K, V]) {
|
||||
m := make(map[K]V)
|
||||
a.ForEach(func(t T) {
|
||||
k, v, ok := fn(t)
|
||||
if ok {
|
||||
_, ok = m[k]
|
||||
if isCoverPrev || !ok {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
})
|
||||
r.m = m
|
||||
return
|
||||
}
|
||||
|
||||
func SimpleStreamFilterAndMap[R, T any](a SimpleSliceStream[T], fn func(T) (R, bool)) SimpleSliceStream[R] {
|
||||
return NewSimpleSliceStream(helper.SliceFilterAndMap(a.arr, fn))
|
||||
}
|
||||
|
@ -560,3 +560,40 @@ func TestSimpleParallelFilterAndMapToMap(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSimpleSliceFilterAndMapToMap(t *testing.T) {
|
||||
type args[T int, K int, V int] struct {
|
||||
a SimpleSliceStream[T]
|
||||
fn func(t T) (K, V, bool)
|
||||
isCoverPrev bool
|
||||
}
|
||||
type testCase[T int, K int, V int] struct {
|
||||
name string
|
||||
args args[T, K, V]
|
||||
wantR SimpleMapStream[K, V]
|
||||
}
|
||||
tests := []testCase[int, int, int]{
|
||||
{
|
||||
name: "t1",
|
||||
args: args[int, int, int]{
|
||||
a: NewSimpleSliceStream(helper.RangeSlice(1, 10, 1)),
|
||||
fn: func(i int) (int, int, bool) {
|
||||
if i > 6 {
|
||||
return i, i, true
|
||||
}
|
||||
return 0, 0, false
|
||||
},
|
||||
},
|
||||
wantR: NewSimpleMapStream(helper.SliceToMap(helper.RangeSlice(7, 10, 1), func(t int) (int, int) {
|
||||
return t, t
|
||||
}, true)),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotR := SimpleSliceFilterAndMapToMap(tt.args.a, tt.args.fn, tt.args.isCoverPrev); !reflect.DeepEqual(gotR, tt.wantR) {
|
||||
t.Errorf("SimpleSliceFilterAndMapToMap() = %v, want %v", gotR, tt.wantR)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user