Skip to content

Commit

Permalink
feat(iterable): improve handling of initial values (accus, fold-inital)
Browse files Browse the repository at this point in the history
  • Loading branch information
kollhof committed Nov 10, 2020
1 parent 5a37472 commit 7770a74
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 45 deletions.
38 changes: 18 additions & 20 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"semantic-release": "^17.2.1"
},
"peerDependencies": {
"@fink/js-interop": ">2.1"
"@fink/js-interop": ">2.2"
},
"dependencies": {
"@babel/core": "^7.10.5",
Expand Down
32 changes: 24 additions & 8 deletions src/lang/iterable/common.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,35 @@ babe_types = import '@babel/types'
{conditionalExpression} = babe_types


{length} = import '@fink/std-lib/iter.fnk'
{length, zip} = import '@fink/std-lib/iter.fnk'

{consts, lets, assign, unique_ident, undef, eq} = import '../../js/types.fnk'
{exprs_block} = import '../block/init.fnk'
{transform} = import '../transform.fnk'



get_initial_values = fn node, args:
[plain_args, initial_args] = match node:
{args: 0 == length ?}:
[[], []]
else:
[item_arg, ...rest] = args
[[item_arg], rest]

[rest_args, initial_values] = pipe initial_args:
map arg: match arg:
{type: 'assign'}:
{left, right} = arg
[left, right]
else:
[arg, {type: 'empty', loc: arg.loc}]
zip ...?

[[...plain_args, ...rest_args], initial_values]



transform_with_runtime_lib = fn iter_fn, node, ctx:
args_len = length node.args

Expand All @@ -34,12 +55,7 @@ transform_with_runtime_lib = fn iter_fn, node, ctx:
async = {type: 'literal', value: 'false', loc: node.loc}
[async, node.args]

[fold_initial, final_args] = match node:
{op: 'fold', args: [, {type: 'assign'}]}:
[first, {left, right}, ...rest] = args
[[right], [first, left, ...rest]]
else:
[[], args]
[final_args, initial_values] = get_initial_values node, args

[result, end_ctx] = transform
rec:
Expand All @@ -54,7 +70,7 @@ transform_with_runtime_lib = fn iter_fn, node, ctx:
{type: 'number', value: '${args_len}', loc: node.loc}
is_async
...use_spread
...fold_initial
...initial_values
loc: node.loc
ctx
[result, end_ctx]
Expand Down
4 changes: 2 additions & 2 deletions src/lang/iterable/filter.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ _filter_((item) => item % 2 === 0, 1, false);"
exports[`filter compiles with custom runtime 2`] = `
"import { _filter_ } from \\"@fink/js-interop/runtime.js\\";
_filter_((item, cntr = 0) => [item < 1 && cntr % 2 === 0, cntr + 1], 2, false);"
_filter_((item, cntr) => [item < 1 && cntr % 2 === 0, cntr + 1], 2, false, 0);"
`;

exports[`filter compiles with default runtime 1`] = `
Expand All @@ -36,7 +36,7 @@ _filter_((item) => item % 2 === 0, 1, false);"
exports[`filter compiles with default runtime 2`] = `
"import { _filter_ } from \\"@fink/js-interop/runtime.js\\";
_filter_((item, cntr = 0) => [item < 1 && cntr % 2 === 0, cntr + 1], 2, false);"
_filter_((item, cntr) => [item < 1 && cntr % 2 === 0, cntr + 1], 2, false, 0);"
`;

exports[`filter legacy compiles 1`] = `
Expand Down
4 changes: 2 additions & 2 deletions src/lang/iterable/fold.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ _fold_((item, prev) => {
exports[`fold compiles with default runtime 2`] = `
"import { _fold_ } from \\"@fink/js-interop/runtime.js\\";
_fold_((item, prev, acc = 1) => [item * acc + prev, acc + 1], 3, false, 0);"
_fold_((item, prev, acc) => [item * acc + prev, acc + 1], 3, false, 0, 1);"
`;

exports[`fold compiles with default runtime 3`] = `
"import { _fold_ } from \\"@fink/js-interop/runtime.js\\";
_fold_((item, prev, acc = 1, shared_acc) => [item * acc + prev, acc + 1, shared_acc + 1], 4, false, 0);"
_fold_((item, prev, acc, shared_acc) => [item * acc + prev, acc + 1, shared_acc + 1], 4, false, 0, 1, undefined);"
`;

exports[`fold compiles with default runtime 4`] = `
Expand Down
4 changes: 2 additions & 2 deletions src/lang/iterable/map.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ exports[`map uses default runtime 1`] = `
{
let ˆpipe_result_1 = [1, 2, 3];
ˆpipe_result_1 = _map_((item) => item * 2, 1, false, false)(ˆpipe_result_1);
ˆpipe_result_1 = _map_((item, acc = 0) => [item + acc, acc + 1], 2, false, false)(ˆpipe_result_1);
ˆpipe_result_1 = _map_((item, acc = 0, shared_acc = 0) => [item + acc + shared_acc, acc + 1, shared_acc + 1], 3, false, false)(ˆpipe_result_1);
ˆpipe_result_1 = _map_((item, acc) => [item + acc, acc + 1], 2, false, false, 0)(ˆpipe_result_1);
ˆpipe_result_1 = _map_((item, acc, shared_acc) => [item + acc + shared_acc, acc + 1, shared_acc + 1], 3, false, false, 0, 0)(ˆpipe_result_1);
ˆpipe_result_1 = _map_((item) => item, 1, false, true)(ˆpipe_result_1);
}"
`;
4 changes: 2 additions & 2 deletions src/lang/iterable/unfold.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ _unfold_((prev = 1) => prev + 1, 1, false, false);"
exports[`unfold compiles with default runtime 4`] = `
"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\";
_unfold_((prev = 1, acc = 1) => [prev * accu, accu + 1], 2, false, false);"
_unfold_((prev = 1, acc) => [prev * accu, accu + 1], 2, false, false, 1);"
`;

exports[`unfold compiles with default runtime 5`] = `
"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\";
_unfold_((prev = 1, acc = 1, shared_accu) => [prev * accu, accu + 1, shared_accu + 1], 3, false, false);"
_unfold_((prev = 1, acc, shared_accu) => [prev * accu, accu + 1, shared_accu + 1], 3, false, false, 1, undefined);"
`;

exports[`unfold legacy compiles with default 1`] = `
Expand Down
4 changes: 2 additions & 2 deletions src/lang/iterable/until.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ _until_((item) => item < 10, 1, false);"
exports[`until compiles with default runtime 2`] = `
"import { _until_ } from \\"@fink/js-interop/runtime.js\\";
_until_((item, cntr = 0) => [cntr > 2, cntr + 1], 2, false);"
_until_((item, cntr) => [cntr > 2, cntr + 1], 2, false, 0);"
`;

exports[`until compiles with default runtime 3`] = `
"import { _until_ } from \\"@fink/js-interop/runtime.js\\";
_until_((item, cntr = 0, shared_accu) => [cntr > 2, cntr + 1, shared_accu + 1], 3, false);"
_until_((item, cntr, shared_accu) => [cntr > 2, cntr + 1, shared_accu + 1], 3, false, 0, undefined);"
`;

exports[`until legacy compiles 1`] = `
Expand Down
4 changes: 2 additions & 2 deletions src/lang/iterable/while.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ _while_((item) => item < 10, 1, false);"
exports[`while compiles with default runtime 2`] = `
"import { _while_ } from \\"@fink/js-interop/runtime.js\\";
_while_((item, cntr = 0) => [cntr > 2, cntr + 1], 2, false);"
_while_((item, cntr) => [cntr > 2, cntr + 1], 2, false, 0);"
`;

exports[`while compiles with default runtime 3`] = `
"import { _while_ } from \\"@fink/js-interop/runtime.js\\";
_while_((item, cntr = 0, shared_accu) => [cntr > 2, cntr + 1, shared_accu + 1], 3, false);"
_while_((item, cntr, shared_accu) => [cntr > 2, cntr + 1, shared_accu + 1], 3, false, 0, undefined);"
`;

exports[`while legacy compiles 1`] = `
Expand Down
6 changes: 3 additions & 3 deletions src/lang/module/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ insert_imports = fn [body, ctx]:
with_ctx ctx
drop_if ignorable_import
map_with_ctx transform_body_expr
collect_with_ctx _
collect_with_ctx ctx

[[...imports, ...body], end_ctx]

Expand All @@ -84,7 +84,7 @@ imported_specs = fn exprs, ctx:
imp = importSpecifier right, left
[imp, , next_ctx]

collect_with_ctx _
collect_with_ctx ctx



Expand Down Expand Up @@ -155,7 +155,7 @@ transform_module = fn node, ctx:
map_with_ctx add_runtime_fn_overrides
drop_if ignorable_import
map_with_ctx transform_body_expr
collect_with_ctx _
collect_with_ctx ctx
insert_imports

js = file
Expand Down
1 change: 0 additions & 1 deletion src/lang/runtime.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ get_runtime_imports = fn ctx:
right: rec:
type: 'string'
exprs: [{type: 'string:text', value: uri}]
[...?]



Expand Down

0 comments on commit 7770a74

Please sign in to comment.