Skip to content

Commit 19173e5

Browse files
authored
#13 - added FoldRev method to collections (#19)
1 parent 0531fcb commit 19173e5

File tree

12 files changed

+249
-41
lines changed

12 files changed

+249
-41
lines changed

base_cases_test.go

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -766,18 +766,18 @@ func getFoldCases(builder baseCollIntBuilder) []*baseTestCase {
766766
coll: builder.Empty(),
767767
args: baseIntArgs{
768768
reducer: func(acc int, i int, current int) int {
769-
return acc + current
769+
return acc*10 + current
770770
},
771-
initial: 0,
771+
initial: 100,
772772
},
773-
want1: 0,
773+
want1: 100,
774774
},
775775
{
776776
name: "Fold() on one-item collection",
777777
coll: builder.One(),
778778
args: baseIntArgs{
779779
reducer: func(acc int, i int, current int) int {
780-
return acc + current
780+
return acc*10 + current
781781
},
782782
initial: 0,
783783
},
@@ -788,22 +788,22 @@ func getFoldCases(builder baseCollIntBuilder) []*baseTestCase {
788788
coll: builder.Three(),
789789
args: baseIntArgs{
790790
reducer: func(acc int, i int, current int) int {
791-
return acc + current
791+
return acc*10 + current
792792
},
793793
initial: 100,
794794
},
795-
want1: 766,
795+
want1: 113653,
796796
},
797797
{
798798
name: "Fold() on three-item collection, include index",
799799
coll: builder.Three(),
800800
args: baseIntArgs{
801801
reducer: func(acc int, i int, current int) int {
802-
return acc + i + current
802+
return acc*(i+1) + current
803803
},
804804
initial: 100,
805805
},
806-
want1: 100 + 0 + 111 + 1 + 222 + 2 + 333,
806+
want1: ((100+111)*2+222)*3 + 333,
807807
},
808808
}
809809
}
@@ -820,6 +820,67 @@ func testFold(t *testing.T, builder baseCollIntBuilder) {
820820
}
821821
}
822822

823+
func getFoldRevCases(builder baseCollIntBuilder) []*baseTestCase {
824+
return []*baseTestCase{
825+
{
826+
name: "FoldRev() on empty collection",
827+
coll: builder.Empty(),
828+
args: baseIntArgs{
829+
reducer: func(acc int, _ int, current int) int {
830+
return acc*10 + current
831+
},
832+
initial: 100,
833+
},
834+
want1: 100,
835+
},
836+
{
837+
name: "FoldRev() on one-item collection",
838+
coll: builder.One(),
839+
args: baseIntArgs{
840+
reducer: func(acc int, _ int, current int) int {
841+
return acc*10 + current
842+
},
843+
initial: 100,
844+
},
845+
want1: 1111,
846+
},
847+
{
848+
name: "FoldRev() on three-item collection",
849+
coll: builder.Three(),
850+
args: baseIntArgs{
851+
reducer: func(acc int, _ int, current int) int {
852+
return acc*10 + current
853+
},
854+
initial: 100,
855+
},
856+
want1: 135631,
857+
},
858+
{
859+
name: "FoldRev() on three-item collection, include index",
860+
coll: builder.Three(),
861+
args: baseIntArgs{
862+
reducer: func(acc int, i int, current int) int {
863+
return acc*(i+1) + current
864+
},
865+
initial: 100,
866+
},
867+
want1: ((100*3+333)*2 + 222) + 111,
868+
},
869+
}
870+
}
871+
872+
func testFoldRev(t *testing.T, builder baseCollIntBuilder) {
873+
cases := getFoldRevCases(builder)
874+
for _, tt := range cases {
875+
t.Run(tt.name, func(t *testing.T) {
876+
got := tt.coll.FoldRev(tt.args.reducer, tt.args.initial)
877+
if !reflect.DeepEqual(got, tt.want1) {
878+
t.Errorf("FoldRev() = %v, want1 %v", got, tt.want1)
879+
}
880+
})
881+
}
882+
}
883+
823884
func getIsEmptyCases(builder baseCollIntBuilder) []baseTestCase {
824885
return []baseTestCase{
825886
{

definitions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type Base[V any] interface {
5858
// See: SearchRev
5959
FindLast(predicate Predicate[V], defaultValue V) V
6060
Fold(reducer Reducer[V], initial V) (result V)
61-
// FoldRev(reducer Reducer[V], initial V) (result V) // TODO
61+
FoldRev(reducer Reducer[V], initial V) (result V)
6262
IsEmpty() bool
6363
Len() int
6464
Search(predicate Predicate[V]) (val V, found bool)

functions.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
11
package coll
22

3-
// public API:
3+
// Public:
44

55
// Copy creates a copy of the given collection.
66
func Copy[C baseInternal[V], V any](c C) C {
7-
//var it V
8-
//
9-
//if _, ok := interface{}(it).(cmp.Cmp); ok {
10-
// // c is of type CmpSequence[V]
11-
//}
12-
137
return c.copy().(C)
14-
15-
//switch v := any(c).(type) {
16-
//case *comfySeq[V]:
17-
// return c.copy().(C)
18-
//case *comfyCmpSeq[any]:
19-
// return c.copy().(C)
20-
//}
21-
22-
// check if c is of type Sequence[C]:
23-
//if cl, ok := any(c).(*comfySeq[V]); ok {
24-
// s := make([]V, len(cl.s))
25-
// for _, v := range cl.s {
26-
// s = append(s, v)
27-
// }
28-
// c := &comfySeq[V]{
29-
// s: s,
30-
// }
31-
// return any(c).(C)
32-
//}
338
}
349

3510
//// Filter creates a new, filtered collection from the given collection.
@@ -38,6 +13,7 @@ func Copy[C baseInternal[V], V any](c C) C {
3813
//}
3914
//
4015
//// MapTo creates a new, mapped collection from the given collection.
16+
// Maybe this should be called "Transform"? Maybe "MapTo" should be an alias for "Transform"?
4117
//func MapTo[OUT Indexed[N], IN Indexed[V], V, N any](coll IN, transformer func(int, V) N) OUT {
4218
// panic("not implemented")
4319
//}

map.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ func (c *comfyMap[K, V]) Fold(reducer Reducer[Pair[K, V]], initial Pair[K, V]) P
126126
return comfyFoldSlice(c.s, reducer, initial)
127127
}
128128

129+
func (c *comfyMap[K, V]) FoldRev(reducer Reducer[Pair[K, V]], initial Pair[K, V]) Pair[K, V] {
130+
return comfyFoldSliceRev(c.s, reducer, initial)
131+
}
132+
129133
func (c *comfyMap[K, V]) Get(k K) (V, bool) {
130134
pair, ok := c.m[k]
131135
if !ok {

map_cases_test.go

Lines changed: 138 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,29 +1201,51 @@ func getMapFoldCases(builder baseMapCollIntBuilder) []baseMapTestCase {
12011201
name: "Fold() on empty collection",
12021202
coll: builder.Empty(),
12031203
args: baseMapIntArgs{
1204-
reducer: func(acc Pair[int, int], i int, current Pair[int, int]) Pair[int, int] {
1204+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
12051205
return acc
12061206
},
12071207
initial: nil,
12081208
},
12091209
want1: nil,
12101210
},
1211+
{
1212+
name: "Fold() on empty collection with initial",
1213+
coll: builder.Empty(),
1214+
args: baseMapIntArgs{
1215+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
1216+
return acc
1217+
},
1218+
initial: NewPair(10, 100),
1219+
},
1220+
want1: NewPair(10, 100),
1221+
},
12111222
{
12121223
name: "Fold() on one-item collection",
12131224
coll: builder.One(),
12141225
args: baseMapIntArgs{
1215-
reducer: func(acc Pair[int, int], i int, current Pair[int, int]) Pair[int, int] {
1226+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
12161227
return current
12171228
},
12181229
initial: nil,
12191230
},
12201231
want1: NewPair(1, 111),
12211232
},
1233+
{
1234+
name: "Fold() on one-item collection with non-nil initial",
1235+
coll: builder.One(),
1236+
args: baseMapIntArgs{
1237+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
1238+
return NewPair(acc.Key()+current.Key(), acc.Val()+current.Val())
1239+
},
1240+
initial: NewPair(10, 100),
1241+
},
1242+
want1: NewPair(11, 211),
1243+
},
12221244
{
12231245
name: "Fold() on three-item collection",
12241246
coll: builder.Three(),
12251247
args: baseMapIntArgs{
1226-
reducer: func(acc Pair[int, int], i int, current Pair[int, int]) Pair[int, int] {
1248+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
12271249
if acc == nil {
12281250
return current
12291251
}
@@ -1233,11 +1255,24 @@ func getMapFoldCases(builder baseMapCollIntBuilder) []baseMapTestCase {
12331255
},
12341256
want1: NewPair(6, 666),
12351257
},
1258+
{
1259+
name: "Fold() on three-item collection with non-nil initial",
1260+
coll: builder.Three(),
1261+
args: baseMapIntArgs{
1262+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
1263+
newKey := acc.Key() + current.Key()
1264+
newVal := acc.Val()*10 + current.Val()
1265+
return NewPair(newKey, newVal)
1266+
},
1267+
initial: NewPair(10, 100),
1268+
},
1269+
want1: NewPair(16, 113653),
1270+
},
12361271
{
12371272
name: "Fold() on six-item collection",
12381273
coll: builder.SixWithDuplicates(),
12391274
args: baseMapIntArgs{
1240-
reducer: func(acc Pair[int, int], i int, current Pair[int, int]) Pair[int, int] {
1275+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
12411276
return NewPair(acc.Key()+current.Key(), acc.Val()+current.Val())
12421277
},
12431278
initial: NewPair(0, 0),
@@ -1259,6 +1294,105 @@ func testMapFold(t *testing.T, builder baseMapCollIntBuilder) {
12591294
}
12601295
}
12611296

1297+
func getMapFoldRevCases(builder baseMapCollIntBuilder) []baseMapTestCase {
1298+
return []baseMapTestCase{
1299+
{
1300+
name: "Fold() on empty collection",
1301+
coll: builder.Empty(),
1302+
args: baseMapIntArgs{
1303+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
1304+
return acc
1305+
},
1306+
initial: nil,
1307+
},
1308+
want1: nil,
1309+
},
1310+
{
1311+
name: "Fold() on empty collection with initial",
1312+
coll: builder.Empty(),
1313+
args: baseMapIntArgs{
1314+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
1315+
return acc
1316+
},
1317+
initial: NewPair(10, 100),
1318+
},
1319+
want1: NewPair(10, 100),
1320+
},
1321+
{
1322+
name: "Fold() on one-item collection",
1323+
coll: builder.One(),
1324+
args: baseMapIntArgs{
1325+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
1326+
return current
1327+
},
1328+
initial: nil,
1329+
},
1330+
want1: NewPair(1, 111),
1331+
},
1332+
{
1333+
name: "Fold() on one-item collection with non-nil initial",
1334+
coll: builder.One(),
1335+
args: baseMapIntArgs{
1336+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
1337+
return NewPair(acc.Key()+current.Key(), acc.Val()+current.Val())
1338+
},
1339+
initial: NewPair(10, 100),
1340+
},
1341+
want1: NewPair(11, 211),
1342+
},
1343+
{
1344+
name: "Fold() on three-item collection",
1345+
coll: builder.Three(),
1346+
args: baseMapIntArgs{
1347+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
1348+
if acc == nil {
1349+
return current
1350+
}
1351+
return NewPair(acc.Key()+current.Key(), acc.Val()+current.Val())
1352+
},
1353+
initial: nil,
1354+
},
1355+
want1: NewPair(6, 666),
1356+
},
1357+
{
1358+
name: "Fold() on three-item collection with non-nil initial",
1359+
coll: builder.Three(),
1360+
args: baseMapIntArgs{
1361+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
1362+
newKey := acc.Key() + current.Key()
1363+
newVal := acc.Val()*10 + current.Val()
1364+
return NewPair(newKey, newVal)
1365+
},
1366+
initial: NewPair(10, 100),
1367+
},
1368+
want1: NewPair(16, 135631),
1369+
},
1370+
{
1371+
name: "Fold() on six-item collection",
1372+
coll: builder.SixWithDuplicates(),
1373+
args: baseMapIntArgs{
1374+
reducer: func(acc Pair[int, int], _ int, current Pair[int, int]) Pair[int, int] {
1375+
return NewPair(acc.Key()+current.Key(), acc.Val()+current.Val())
1376+
},
1377+
initial: NewPair(0, 0),
1378+
},
1379+
want1: NewPair(21, 1332),
1380+
},
1381+
}
1382+
}
1383+
1384+
func testMapFoldRev(t *testing.T, builder baseMapCollIntBuilder) {
1385+
cases := getMapFoldRevCases(builder)
1386+
for _, tt := range cases {
1387+
t.Run(tt.name, func(t *testing.T) {
1388+
got := tt.coll.FoldRev(tt.args.reducer, tt.args.initial)
1389+
if !reflect.DeepEqual(got, tt.want1) {
1390+
t.Errorf("FoldRev() = %v, want1 = %v", got, tt.want1)
1391+
}
1392+
})
1393+
}
1394+
}
1395+
12621396
func getMapGetCases(builder baseMapCollIntBuilder) []baseMapTestCase {
12631397
return []baseMapTestCase{
12641398
{

map_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ func Test_comfyMap_Fold(t *testing.T) {
209209
testMapFold(t, &comfyMapIntBuilder[mapInternal[int, int]]{})
210210
}
211211

212+
func Test_comfyMap_FoldRev(t *testing.T) {
213+
testMapFoldRev(t, &comfyMapIntBuilder[mapInternal[int, int]]{})
214+
}
215+
212216
func Test_comfyMap_Get(t *testing.T) {
213217
testMapGet(t, &comfyMapIntBuilder[mapInternal[int, int]]{})
214218
}

0 commit comments

Comments
 (0)