Skip to content

Commit c593ed8

Browse files
authored
Merge pull request #33 from fink-lang/fold-with-initial-ctx
feat(iter): add support for initial shared accu for fold_ac
2 parents 049ed53 + f39e0dc commit c593ed8

File tree

3 files changed

+61
-38
lines changed

3 files changed

+61
-38
lines changed

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/iter.fnk

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ sort = _sort_
2020

2121

2222

23-
fold = fn initial, fold_fn=initial:
23+
fold = fn initial, fold_fn:
2424
foldr = fn iterable, prev, ctx, fold_fn:
2525
[item, rest, next_ctx=ctx] = _next_ iterable, ctx
2626
match rest:
@@ -31,13 +31,14 @@ fold = fn initial, fold_fn=initial:
3131

3232
fn items:
3333
it = _iter_ items
34-
[init, ff] = match initial:
35-
fold_fn: [, fold_fn]
36-
else: [initial, fold_fn]
37-
foldr it, init, _, ff
34+
match true:
35+
is_fn fold_fn:
36+
foldr it, initial, _, fold_fn
37+
else:
38+
foldr it, _, _, initial
3839

3940

40-
fold_ac = fn initial, fold_fn=initial:
41+
fold_ac = fn initial, ctx, fold_fn:
4142
foldr = fn iterable, prev, acc, ctx, fold_fn:
4243
[item, rest, f_ctx=ctx] = _next_ iterable, ctx
4344
match rest:
@@ -48,10 +49,16 @@ fold_ac = fn initial, fold_fn=initial:
4849

4950
fn items:
5051
it = _iter_ items
51-
[init, ff] = match initial:
52-
fold_fn: [, fold_fn]
53-
else: [initial, fold_fn]
54-
foldr it, init, _, _, ff
52+
uses_ctx = is_fn fold_fn
53+
54+
acc_ctx_fold_fn = match true:
55+
is_fn fold_fn: [ , ctx, fold_fn]
56+
else: [ , , ctx]
57+
58+
[result, next_ctx] = foldr it, initial, ...acc_ctx_fold_fn
59+
match true:
60+
uses_ctx: [result, next_ctx]
61+
else: result
5562

5663

5764

@@ -109,6 +116,7 @@ filter_ac = fn filter_fn:
109116

110117

111118

119+
112120
while = fn while_fn:
113121
next_fn = fn iterable, ctx:
114122
[item, rest, next_ctx=ctx] = _next_ iterable, ctx

src/iter.test.fnk

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ expect_iter = fn actual, expectation:
2424
describe 'fold', fn:
2525
it 'folds items', fn:
2626
expect
27-
'123' | fold_ fn item, prev='':
27+
'123' | fold_ '', fn item, prev:
2828
'${item}${prev}'
2929
to_equal
3030
'321'
@@ -36,22 +36,37 @@ describe 'fold', fn:
3636
to_equal
3737
'foobar'
3838

39+
it 'folds without initial', fn:
40+
expect
41+
'123' | fold_ fn item, prev='':
42+
'${item}${prev}'
43+
to_equal
44+
'321'
45+
3946

4047
describe fold_ac, fn:
4148
it 'folds items with accu', fn:
4249
expect
43-
'abc' | fold_ac fn item, prev='', acc=0:
50+
'abc' | fold_ac '', fn item, prev, acc=0:
4451
['${prev},${item}${acc}', acc + 1]
4552
to_equal
46-
[',a0,b1,c2', ]
53+
',a0,b1,c2'
4754

48-
it 'folds items with shared accu', fn:
55+
it 'folds with shared accu', fn:
4956
expect
50-
pipe 'abc':
51-
fold_ac '.', fn item, prev, , cntr=0:
57+
pipe '123':
58+
fold_ac '.', 0, fn item, prev, , cntr:
5259
['${prev}${item}', , cntr + 1]
5360
to_equal
54-
['.abc', 3]
61+
['.123', 3]
62+
63+
it 'folds empty with shared accu', fn:
64+
expect
65+
pipe '':
66+
fold_ac '', 0, fn item, prev, , cntr:
67+
['${prev}${item}', , cntr + 1]
68+
to_equal
69+
['', 0]
5570

5671

5772

@@ -333,9 +348,9 @@ describe flatten, fn:
333348
expect
334349
pipe 'abc':
335350
map_ac fn item, , ctx=0:
336-
[[ctx, item],, ctx + 1]
351+
[[ctx, item], , ctx + 1]
337352
flatten
338-
fold_ fn it, out=[], , ctx:
353+
fold_ [], fn it, out, , ctx:
339354
[...out, '${it}${ctx}']
340355
to_equal
341356
["01", "a1", "12", "b2", "23", "c3"]
@@ -747,12 +762,12 @@ describe 'performance', fn:
747762
zip ?, count 1000
748763
map fn [item, cntr]: {cntr, item}
749764
filter fn {cntr}: cntr % 2 == 0
750-
fold_ac 0, fn _, c=0: [c + 1]
751-
fn [a]: a
765+
fold_ac 0, fn _, c: [c + 1]
766+
752767

753768
t2 = fn:
754769
pipe range 100:
755-
fold_ fn: t1 _
770+
fold_ 0, fn: t1 _
756771

757772
expect
758773
t2 _

0 commit comments

Comments
 (0)