Skip to content

Commit

Permalink
Merge pull request #34 from fink-lang/fix-iter-shared-accu
Browse files Browse the repository at this point in the history
feat(iter): add shared context injector, fix ctx pass through
  • Loading branch information
kollhof authored Jul 12, 2021
2 parents c593ed8 + 1b5f3db commit 784a13a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 23 deletions.
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions src/iter.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ sort = _sort_



with_ctx = fn initial_ctx:
next_fn = fn iterable, ctx=initial_ctx:
_next_ iterable, ctx

fn items: _iterable_ next_fn, _iter_ items



fold = fn initial, fold_fn:
foldr = fn iterable, prev, ctx, fold_fn:
[item, rest, next_ctx=ctx] = _next_ iterable, ctx
Expand All @@ -44,7 +52,7 @@ fold_ac = fn initial, ctx, fold_fn:
match rest:
_is_done_ ?: [prev, f_ctx]
else:
[result, next_acc, next_ctx] = fold_fn item, prev, acc, f_ctx
[result, next_acc, next_ctx=f_ctx] = fold_fn item, prev, acc, f_ctx
foldr rest, result, next_acc, next_ctx, fold_fn

fn items:
Expand Down Expand Up @@ -100,11 +108,11 @@ filter = fn filter_fn:

filter_ac = fn filter_fn:
next_fn = fn [iterable, acc], ctx:
[item, rest, m_ctx=ctx] = _next_ iterable, ctx
[item, rest, f_ctx=ctx] = _next_ iterable, ctx
match rest:
_is_done_ ?: [item, rest, m_ctx]
_is_done_ ?: [item, rest, f_ctx]
else:
[keep, next_acc, next_ctx] = filter_fn item, acc, m_ctx
[keep, next_acc, next_ctx=f_ctx] = filter_fn item, acc, f_ctx
match keep:
true:
next_iter = _iterable_ next_fn, [rest, next_acc]
Expand Down Expand Up @@ -171,7 +179,7 @@ map_ac = fn map_fn:
match rest:
_is_done_ ?: [item, rest, m_ctx]
else:
[mitem, next_acc, next_ctx] = map_fn item, acc, m_ctx
[mitem, next_acc, next_ctx=m_ctx] = map_fn item, acc, m_ctx
next_iter = _iterable_ next_fn, [rest, next_acc]
[mitem, next_iter, next_ctx]

Expand Down
56 changes: 52 additions & 4 deletions src/iter.test.fnk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{describe, it, expect, to_equal} = import '@fink/jest/test.fnk'

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

{
repeat, count, enumerate, zip, cycle, chain, reverse, sort, unique, flatten
Expand Down Expand Up @@ -52,22 +52,31 @@ describe fold_ac, fn:
to_equal
',a0,b1,c2'

it 'folds with shared accu', fn:
it 'folds with shared context', fn:
expect
pipe '123':
fold_ac '.', 0, fn item, prev, , cntr:
['${prev}${item}', , cntr + 1]
to_equal
['.123', 3]

it 'folds empty with shared accu', fn:
it 'folds empty with shared context', fn:
expect
pipe '':
fold_ac '', 0, fn item, prev, , cntr:
['${prev}${item}', , cntr + 1]
to_equal
['', 0]

it 'folds without losing shared context', fn:
expect
pipe 'abc':
map_ac fn item, , cntr:
[item, , cntr + 1]
fold_ac '', 0, fn item, prev:
['${prev}${item}']
to_equal
['abc', 3]


describe 'unfold', fn:
Expand All @@ -93,7 +102,7 @@ describe unfold_ac, fn:
to_equal
[1, 3, 7, 15]

it 'creates items with shared accu', fn:
it 'creates items with shared context', fn:
expect_iter
pipe 0:
unfold_ac fn prev, , ctx='.':
Expand Down Expand Up @@ -133,6 +142,19 @@ describe map_ac, fn:
to_equal
[1, 3, 5, 7]

it 'folds without losing shared context', fn:
expect
pipe 'abc':
map_ac fn item, , cntr:
[item, , cntr + 1]

map_ac fn item, , cntr:
['${item}${cntr}']

fold_ac '', 0, fn item, prev:
['${prev}${item}']
to_equal
['a1b2c3', 3]


describe 'filter', fn:
Expand All @@ -159,6 +181,20 @@ describe filter_ac, fn:
to_equal
[2, 3]

it 'folds without losing shared context', fn:
expect
pipe 'abbc':
map_ac fn item, , cntr:
[item, , cntr + 1]

filter_ac fn item, , cntr:
[item != 'b' and cntr != 2]

fold_ac '', 0, fn item, prev:
['${prev}${item}']

to_equal
['ac', 4]


describe 'while', fn:
Expand Down Expand Up @@ -187,6 +223,18 @@ describe while_ac, fn:



describe with_ctx, fn:
it 'sets up shared context', fn:
expect_iter
pipe 'abc':
with_ctx 0
map_ac fn item, , ctx:
['${item}${ctx}', , ctx + 1]
to_equal
['a0', 'b1', 'c2']



describe repeat, fn:
it 'repeats item n times', fn:
expect_iter
Expand Down

0 comments on commit 784a13a

Please sign in to comment.