Skip to content

Commit f2d566d

Browse files
authored
Feat/#13/reduce rev (#21)
* #13 - added `FoldRev` method to collections * #13 - added `ReduceRev` method to collections
1 parent 19173e5 commit f2d566d

File tree

12 files changed

+138
-23
lines changed

12 files changed

+138
-23
lines changed

README.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
= Comfy Gopher - Collections
22

33
image:https://codecov.io/gh/comfygopher/collections/graph/badge.svg?token=I5QQ2SU3E7[codecov,link=https://codecov.io/gh/comfygopher/collections]
4-
5-
image:https://img.shields.io/coderabbit/prs/github/comfygopher/collections?utm_source=oss&utm_medium=github&utm_campaign=comfygopher%2Fcollections&labelColor=171717&color=FF570A&link=https%3A%2F%2Fcoderabbit.ai&label=CodeRabbit+Reviews[CodeRabbit Pull Request Reviews]
4+
image:https://img.shields.io/coderabbit/prs/github/comfygopher/collections?utm_source=oss&utm_medium=github&utm_campaign=comfygopher%2Fcollections&labelColor=5b5b5b&color=FF570A&link=https%3A%2F%2Fcoderabbit.ai&label=CodeRabbit+Reviews[CodeRabbit Pull Request Reviews]
5+
image:https://goreportcard.com/badge/github.com/comfygopher/collections[Go Report Card,link=https://goreportcard.com/report/github.com/comfygopher/collections]
66

77
== What is Comfy Gopher?
88

base_cases_test.go

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -950,47 +950,85 @@ func getReduceCases(t *testing.T, builder baseCollIntBuilder) []*baseTestCase {
950950
{
951951
name: "Reduce() on empty collection",
952952
coll: builder.Empty(),
953-
args: baseIntArgs{reducer: func(acc int, i int, current int) int {
954-
return acc + current
953+
args: baseIntArgs{reducer: func(acc int, _ int, current int) int {
954+
return acc*10 + current
955955
}},
956956
want1: 0,
957957
want2: ErrEmptyCollection,
958958
},
959959
{
960960
name: "Fold() on one-item collection",
961961
coll: builder.One(),
962-
args: baseIntArgs{reducer: func(acc int, i int, current int) int {
963-
return acc + current
962+
args: baseIntArgs{reducer: func(acc int, _ int, current int) int {
963+
return acc*10 + current
964964
}},
965965
want1: 111,
966966
want2: nil,
967967
},
968968
{
969969
name: "Fold() on three-item collection",
970970
coll: builder.Three(),
971-
args: baseIntArgs{reducer: func(acc int, i int, current int) int {
972-
return acc + current
971+
args: baseIntArgs{reducer: func(acc int, _ int, current int) int {
972+
return acc*10 + current
973973
}},
974-
want1: 666,
974+
want1: 13653,
975975
want2: nil,
976976
},
977+
}
978+
}
979+
980+
func testReduce(t *testing.T, builder baseCollIntBuilder) {
981+
cases := getReduceCases(t, builder)
982+
for _, tt := range cases {
983+
t.Run(tt.name, func(t *testing.T) {
984+
got1, got2 := tt.coll.Reduce(tt.args.reducer)
985+
if !reflect.DeepEqual(got1, tt.want1) {
986+
t.Errorf("Reduce() = %v, want1 %v", got1, tt.want1)
987+
}
988+
if !reflect.DeepEqual(got2, tt.want2) {
989+
t.Errorf("Reduce() = %v, want1 %v", got2, tt.want2)
990+
}
991+
})
992+
}
993+
}
994+
995+
func getReduceRevCases(t *testing.T, builder baseCollIntBuilder) []*baseTestCase {
996+
return []*baseTestCase{
977997
{
978-
name: "Fold() on three-item collection, include index",
998+
name: "Reduce() on empty collection",
999+
coll: builder.Empty(),
1000+
args: baseIntArgs{reducer: func(acc int, _ int, current int) int {
1001+
return acc*10 + current
1002+
}},
1003+
want1: 0,
1004+
want2: ErrEmptyCollection,
1005+
},
1006+
{
1007+
name: "Fold() on one-item collection",
1008+
coll: builder.One(),
1009+
args: baseIntArgs{reducer: func(acc int, _ int, current int) int {
1010+
return acc*10 + current
1011+
}},
1012+
want1: 111,
1013+
want2: nil,
1014+
},
1015+
{
1016+
name: "Fold() on three-item collection",
9791017
coll: builder.Three(),
980-
args: baseIntArgs{reducer: func(acc int, i int, current int) int {
981-
return acc + i + current
1018+
args: baseIntArgs{reducer: func(acc int, _ int, current int) int {
1019+
return acc*10 + current
9821020
}},
983-
want1: 0 + 111 + 1 + 222 + 2 + 333,
1021+
want1: 35631,
9841022
want2: nil,
9851023
},
9861024
}
9871025
}
9881026

989-
func testReduce(t *testing.T, builder baseCollIntBuilder) {
990-
cases := getReduceCases(t, builder)
1027+
func testReduceRev(t *testing.T, builder baseCollIntBuilder) {
1028+
cases := getReduceRevCases(t, builder)
9911029
for _, tt := range cases {
9921030
t.Run(tt.name, func(t *testing.T) {
993-
got1, got2 := tt.coll.Reduce(tt.args.reducer)
1031+
got1, got2 := tt.coll.ReduceRev(tt.args.reducer)
9941032
if !reflect.DeepEqual(got1, tt.want1) {
9951033
t.Errorf("Reduce() = %v, want1 %v", got1, tt.want1)
9961034
}

definitions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type Base[V any] interface {
6666
// SearchPos(predicate Predicate[V]) (val V, found bool) // TODO
6767
SearchRev(predicate Predicate[V]) (val V, found bool)
6868
Reduce(reducer Reducer[V]) (result V, err error)
69-
// ReduceRev(reducer Reducer[V]) (result V, err error) // TODO
69+
ReduceRev(reducer Reducer[V]) (result V, err error)
7070
ToSlice() []V
7171
Values() iter.Seq[V]
7272
// ValuesRev() iter.Seq[V] // TODO

map.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ func (c *comfyMap[K, V]) Reduce(reducer Reducer[Pair[K, V]]) (Pair[K, V], error)
206206
return comfyReduceSlice(c.s, reducer)
207207
}
208208

209+
func (c *comfyMap[K, V]) ReduceRev(reducer Reducer[Pair[K, V]]) (Pair[K, V], error) {
210+
return comfyReduceSliceRev(c.s, reducer)
211+
}
212+
209213
func (c *comfyMap[K, V]) Remove(k K) {
210214
c.remove(k)
211215
}

map_cases_test.go

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,30 +2051,30 @@ func testMapPrepend(t *testing.T, builder baseMapCollIntBuilder) {
20512051
}
20522052

20532053
func getMapReduceCases(builder baseMapCollIntBuilder) []baseMapTestCase {
2054-
sumReducer := func(acc Pair[int, int], i int, current Pair[int, int]) Pair[int, int] {
2055-
return NewPair(acc.Key()+current.Key(), acc.Val()+current.Val())
2054+
reducer := func(acc Pair[int, int], i int, current Pair[int, int]) Pair[int, int] {
2055+
return NewPair(acc.Key()+current.Key(), acc.Val()*10+current.Val())
20562056
}
20572057

20582058
return []baseMapTestCase{
20592059
{
20602060
name: "Reduce() on empty collection",
20612061
coll: builder.Empty(),
2062-
args: baseMapIntArgs{reducer: sumReducer},
2062+
args: baseMapIntArgs{reducer: reducer},
20632063
want1: nil,
20642064
want2: ErrEmptyCollection,
20652065
},
20662066
{
20672067
name: "Reduce() on one-item collection",
20682068
coll: builder.One(),
2069-
args: baseMapIntArgs{reducer: sumReducer},
2069+
args: baseMapIntArgs{reducer: reducer},
20702070
want1: NewPair(1, 111),
20712071
want2: nil,
20722072
},
20732073
{
20742074
name: "Reduce() on three-item collection",
20752075
coll: builder.Three(),
2076-
args: baseMapIntArgs{reducer: sumReducer},
2077-
want1: NewPair(6, 666),
2076+
args: baseMapIntArgs{reducer: reducer},
2077+
want1: NewPair(6, 13653),
20782078
want2: nil,
20792079
},
20802080
}
@@ -2095,6 +2095,51 @@ func testMapReduce(t *testing.T, builder baseMapCollIntBuilder) {
20952095
}
20962096
}
20972097

2098+
func getMapReduceRevCases(builder baseMapCollIntBuilder) []baseMapTestCase {
2099+
reducer := func(acc Pair[int, int], i int, current Pair[int, int]) Pair[int, int] {
2100+
return NewPair(acc.Key()+current.Key(), acc.Val()*10+current.Val())
2101+
}
2102+
2103+
return []baseMapTestCase{
2104+
{
2105+
name: "Reduce() on empty collection",
2106+
coll: builder.Empty(),
2107+
args: baseMapIntArgs{reducer: reducer},
2108+
want1: nil,
2109+
want2: ErrEmptyCollection,
2110+
},
2111+
{
2112+
name: "Reduce() on one-item collection",
2113+
coll: builder.One(),
2114+
args: baseMapIntArgs{reducer: reducer},
2115+
want1: NewPair(1, 111),
2116+
want2: nil,
2117+
},
2118+
{
2119+
name: "Reduce() on three-item collection",
2120+
coll: builder.Three(),
2121+
args: baseMapIntArgs{reducer: reducer},
2122+
want1: NewPair(6, 35631),
2123+
want2: nil,
2124+
},
2125+
}
2126+
}
2127+
2128+
func testMapReduceRev(t *testing.T, builder baseMapCollIntBuilder) {
2129+
cases := getMapReduceRevCases(builder)
2130+
for _, tt := range cases {
2131+
t.Run(tt.name, func(t *testing.T) {
2132+
got1, got2 := tt.coll.ReduceRev(tt.args.reducer)
2133+
if !reflect.DeepEqual(got1, tt.want1) {
2134+
t.Errorf("Reduce() got1 = %v, want1 = %v", got1, tt.want1)
2135+
}
2136+
if !reflect.DeepEqual(got2, tt.want2) {
2137+
t.Errorf("Reduce() got2 = %v, want2 = %v", got2, tt.want2)
2138+
}
2139+
})
2140+
}
2141+
}
2142+
20982143
func getMapRemoveCases(builder baseMapCollIntBuilder) []baseMapTestCase {
20992144
return []baseMapTestCase{
21002145
{

map_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ func Test_comfyMap_Reduce(t *testing.T) {
262262
testMapReduce(t, &comfyMapIntBuilder[mapInternal[int, int]]{})
263263
}
264264

265+
func Test_comfyMap_ReduceRev(t *testing.T) {
266+
testMapReduceRev(t, &comfyMapIntBuilder[mapInternal[int, int]]{})
267+
}
268+
265269
func Test_comfyMap_Remove(t *testing.T) {
266270
testMapRemove(t, &comfyMapIntBuilder[mapInternal[int, int]]{})
267271
}

mapcmp.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ func (c *comfyCmpMap[K, V]) Reduce(reducer Reducer[Pair[K, V]]) (Pair[K, V], err
255255
return comfyReduceSlice(c.s, reducer)
256256
}
257257

258+
func (c *comfyCmpMap[K, V]) ReduceRev(reducer Reducer[Pair[K, V]]) (Pair[K, V], error) {
259+
return comfyReduceSliceRev(c.s, reducer)
260+
}
261+
258262
func (c *comfyCmpMap[K, V]) Remove(k K) {
259263
c.remove(k)
260264
}

mapcmp_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ func Test_comfyCmpMap_Reduce(t *testing.T) {
310310
testMapReduce(t, &comfyCmpMapIntBuilder[mapInternal[int, int]]{})
311311
}
312312

313+
func Test_comfyCmpMap_ReduceRev(t *testing.T) {
314+
testMapReduceRev(t, &comfyCmpMapIntBuilder[mapInternal[int, int]]{})
315+
}
316+
313317
func Test_comfyCmpMap_Remove(t *testing.T) {
314318
testMapRemove(t, &comfyCmpMapIntBuilder[mapInternal[int, int]]{})
315319
}

sequence.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ func (c *comfySeq[V]) Reduce(reducer Reducer[V]) (V, error) {
177177
return comfyReduceSlice(c.s, reducer)
178178
}
179179

180+
func (c *comfySeq[V]) ReduceRev(reducer Reducer[V]) (V, error) {
181+
return comfyReduceSliceRev(c.s, reducer)
182+
}
183+
180184
func (c *comfySeq[V]) RemoveAt(i int) (removed V, err error) {
181185
if removed, c.s, err = sliceRemoveAt(c.s, i); err != nil {
182186
return removed, err

sequence_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ func Test_comfySeq_Reduce(t *testing.T) {
228228
testReduce(t, &comfySeqIntBuilder[baseInternal[int]]{})
229229
}
230230

231+
func Test_comfySeq_ReduceRev(t *testing.T) {
232+
testReduceRev(t, &comfySeqIntBuilder[baseInternal[int]]{})
233+
}
234+
231235
func Test_comfySeq_RemoveAt(t *testing.T) {
232236
testRemoveAt(t, &comfySeqIntBuilder[indexedMutableInternal[int]]{})
233237
}

0 commit comments

Comments
 (0)