Skip to content

Commit

Permalink
Merge pull request #16 from fink-lang/find
Browse files Browse the repository at this point in the history
feat(find): implement `find item: ...`
  • Loading branch information
kollhof authored Mar 3, 2020
2 parents d74514b + 0beb5ba commit d034771
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 10 deletions.
12 changes: 6 additions & 6 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 @@ -38,7 +38,7 @@
"@babel/cli": "^7.2.3",
"@babel/plugin-proposal-pipeline-operator": "^7.3.2",
"@babel/preset-env": "^7.8.6",
"@fink/larix": "^4.2.0",
"@fink/larix": "^4.3.0",
"@nearmap/eslint-config-base": "^1.1.0",
"babel-eslint": "^10.1.0",
"commitizen": "^4.0.3",
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {transform_map} from './transform/map';
import {transform_flat_map} from './transform/map';
import {transform_filter} from './transform/filter';
import {transform_while} from './transform/while';
import {transform_find} from './transform/find';
import {transform_fold} from './transform/fold';
import {transform_unfold} from './transform/unfold';
import {transform_call} from './transform/call';
Expand All @@ -33,6 +34,8 @@ import {transform_inifx} from './transform/infix';
import {transform_import} from './transform/import';
import {transform_ident, escape_ident, var_prefix} from './transform/other';
import {transform_number} from './transform/number';
import {transform_new} from './transform/new';
import {transform_throw} from './transform/throw';

import {
transform_jsx_elem, transform_jsx_attr, transform_jsx_str,
Expand All @@ -41,8 +44,6 @@ import {

import transform_do_expr from './transform/js/do-expression';
import {transform_async} from './transform/js/async';
import {transform_new} from './transform/new';
import {transform_throw} from './transform/throw';


const jsx = {
Expand Down Expand Up @@ -113,7 +114,8 @@ const iterables = {
map: transform_map,
flat_map: transform_flat_map,
filter: transform_filter,
while: transform_while
while: transform_while,
find: transform_find
};


Expand Down
55 changes: 55 additions & 0 deletions src/transform/__snapshots__/find.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compiles fold compiles complex find 1`] = `
"ˆitems_2 => {
for (const ˆitem_1 of ˆitems_2) {
const {
\\"item\\": item
} = ˆitem_1;
let _do_result;
ˆmatch_5: {
const ˆvalue_4 = item;
if (ˆvalue_4 !== undefined && ˆvalue_4 !== null) {
const {
\\"spam\\": ˆp_6
} = ˆvalue_4;
ifp_6 === spam) {
_do_result = shrub;
break ˆmatch_5;
}
}
{
_do_result = ni;
break ˆmatch_5;
}
}
const ˆfound_3 = _do_result;
_do_result = undefined;
if (ˆfound_3) return ˆitem_1;
}
return undefined;
};
Object.assign(module.exports, {});"
`;

exports[`compiles fold compiles simple find 1`] = `
"ˆitems_2 => {
for (const ˆitem_1 of ˆitems_2) {
const item = ˆitem_1;
const ˆfound_3 = item > 3;
if (ˆfound_3) return ˆitem_1;
}
return undefined;
};
Object.assign(module.exports, {});"
`;
34 changes: 34 additions & 0 deletions src/transform/find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
params, for_of, split_last, func, returns, iff, consts, undef
} from '../types';
import {block_statement} from './block';


export const transform_find = (node, {transform, unique_ident})=> {
const [item_param] = node.args.map(transform);
const [init] = params([item_param]);

const item = unique_ident('item');
const items = unique_ident('items');
const found = unique_ident('found');

const [expressions, last_expr] = split_last(node.exprs);

return func(items)(

for_of(item, items)(
consts(init, item),

...expressions.map(block_statement({transform})),

consts(found, transform(last_expr)),

iff(found)(
returns(item)
)
),

undef()
);
};

26 changes: 26 additions & 0 deletions src/transform/find.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {fink2js} from '../testing';


describe('compiles fold', ()=> {
it('compiles simple find', ()=> {
expect(
fink2js(`
find item:
item > 3
`)
).toMatchSnapshot();
});

it('compiles complex find', ()=> {
expect(
fink2js(`
find {item}:
match item:
{spam}: shrub
else: ni
`)
).toMatchSnapshot();
});
});


0 comments on commit d034771

Please sign in to comment.