Skip to content

Commit 784a13a

Browse files
authored
Merge pull request #34 from fink-lang/fix-iter-shared-accu
feat(iter): add shared context injector, fix ctx pass through
2 parents c593ed8 + 1b5f3db commit 784a13a

File tree

3 files changed

+79
-23
lines changed

3 files changed

+79
-23
lines changed

package-lock.json

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

src/iter.fnk

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ sort = _sort_
2020

2121

2222

23+
with_ctx = fn initial_ctx:
24+
next_fn = fn iterable, ctx=initial_ctx:
25+
_next_ iterable, ctx
26+
27+
fn items: _iterable_ next_fn, _iter_ items
28+
29+
30+
2331
fold = fn initial, fold_fn:
2432
foldr = fn iterable, prev, ctx, fold_fn:
2533
[item, rest, next_ctx=ctx] = _next_ iterable, ctx
@@ -44,7 +52,7 @@ fold_ac = fn initial, ctx, fold_fn:
4452
match rest:
4553
_is_done_ ?: [prev, f_ctx]
4654
else:
47-
[result, next_acc, next_ctx] = fold_fn item, prev, acc, f_ctx
55+
[result, next_acc, next_ctx=f_ctx] = fold_fn item, prev, acc, f_ctx
4856
foldr rest, result, next_acc, next_ctx, fold_fn
4957

5058
fn items:
@@ -100,11 +108,11 @@ filter = fn filter_fn:
100108

101109
filter_ac = fn filter_fn:
102110
next_fn = fn [iterable, acc], ctx:
103-
[item, rest, m_ctx=ctx] = _next_ iterable, ctx
111+
[item, rest, f_ctx=ctx] = _next_ iterable, ctx
104112
match rest:
105-
_is_done_ ?: [item, rest, m_ctx]
113+
_is_done_ ?: [item, rest, f_ctx]
106114
else:
107-
[keep, next_acc, next_ctx] = filter_fn item, acc, m_ctx
115+
[keep, next_acc, next_ctx=f_ctx] = filter_fn item, acc, f_ctx
108116
match keep:
109117
true:
110118
next_iter = _iterable_ next_fn, [rest, next_acc]
@@ -171,7 +179,7 @@ map_ac = fn map_fn:
171179
match rest:
172180
_is_done_ ?: [item, rest, m_ctx]
173181
else:
174-
[mitem, next_acc, next_ctx] = map_fn item, acc, m_ctx
182+
[mitem, next_acc, next_ctx=m_ctx] = map_fn item, acc, m_ctx
175183
next_iter = _iterable_ next_fn, [rest, next_acc]
176184
[mitem, next_iter, next_ctx]
177185

src/iter.test.fnk

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{describe, it, expect, to_equal} = import '@fink/jest/test.fnk'
22

33
{fold: fold_, unfold, map, filter, while} = import './iter.fnk'
4-
{fold_ac, unfold_ac, map_ac, filter_ac, while_ac} = import './iter.fnk'
4+
{with_ctx, fold_ac, unfold_ac, map_ac, filter_ac, while_ac} = import './iter.fnk'
55

66
{
77
repeat, count, enumerate, zip, cycle, chain, reverse, sort, unique, flatten
@@ -52,22 +52,31 @@ describe fold_ac, fn:
5252
to_equal
5353
',a0,b1,c2'
5454

55-
it 'folds with shared accu', fn:
55+
it 'folds with shared context', fn:
5656
expect
5757
pipe '123':
5858
fold_ac '.', 0, fn item, prev, , cntr:
5959
['${prev}${item}', , cntr + 1]
6060
to_equal
6161
['.123', 3]
6262

63-
it 'folds empty with shared accu', fn:
63+
it 'folds empty with shared context', fn:
6464
expect
6565
pipe '':
6666
fold_ac '', 0, fn item, prev, , cntr:
6767
['${prev}${item}', , cntr + 1]
6868
to_equal
6969
['', 0]
7070

71+
it 'folds without losing shared context', fn:
72+
expect
73+
pipe 'abc':
74+
map_ac fn item, , cntr:
75+
[item, , cntr + 1]
76+
fold_ac '', 0, fn item, prev:
77+
['${prev}${item}']
78+
to_equal
79+
['abc', 3]
7180

7281

7382
describe 'unfold', fn:
@@ -93,7 +102,7 @@ describe unfold_ac, fn:
93102
to_equal
94103
[1, 3, 7, 15]
95104

96-
it 'creates items with shared accu', fn:
105+
it 'creates items with shared context', fn:
97106
expect_iter
98107
pipe 0:
99108
unfold_ac fn prev, , ctx='.':
@@ -133,6 +142,19 @@ describe map_ac, fn:
133142
to_equal
134143
[1, 3, 5, 7]
135144

145+
it 'folds without losing shared context', fn:
146+
expect
147+
pipe 'abc':
148+
map_ac fn item, , cntr:
149+
[item, , cntr + 1]
150+
151+
map_ac fn item, , cntr:
152+
['${item}${cntr}']
153+
154+
fold_ac '', 0, fn item, prev:
155+
['${prev}${item}']
156+
to_equal
157+
['a1b2c3', 3]
136158

137159

138160
describe 'filter', fn:
@@ -159,6 +181,20 @@ describe filter_ac, fn:
159181
to_equal
160182
[2, 3]
161183

184+
it 'folds without losing shared context', fn:
185+
expect
186+
pipe 'abbc':
187+
map_ac fn item, , cntr:
188+
[item, , cntr + 1]
189+
190+
filter_ac fn item, , cntr:
191+
[item != 'b' and cntr != 2]
192+
193+
fold_ac '', 0, fn item, prev:
194+
['${prev}${item}']
195+
196+
to_equal
197+
['ac', 4]
162198

163199

164200
describe 'while', fn:
@@ -187,6 +223,18 @@ describe while_ac, fn:
187223

188224

189225

226+
describe with_ctx, fn:
227+
it 'sets up shared context', fn:
228+
expect_iter
229+
pipe 'abc':
230+
with_ctx 0
231+
map_ac fn item, , ctx:
232+
['${item}${ctx}', , ctx + 1]
233+
to_equal
234+
['a0', 'b1', 'c2']
235+
236+
237+
190238
describe repeat, fn:
191239
it 'repeats item n times', fn:
192240
expect_iter

0 commit comments

Comments
 (0)