Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JS Set #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Add JS Set #60

wants to merge 3 commits into from

Conversation

jayvdb
Copy link

@jayvdb jayvdb commented May 30, 2021

Fixes #24

@jayvdb
Copy link
Author

jayvdb commented May 30, 2021

I expect that I have missed some obvious things, and that the implementation is not efficient esp using Array.from unnecessarily. I'm happy to amend this based on guidance from people who know the codebase better, for feel free to merge and fix it if that is less effort for the maintainers. I know that there are still some fairly basic scenarios not covered, e.g. the older syntax set([1]) | set([1]) doesn't trigger op_set_*, and len(set()) doesnt work just like len({}) doesnt work for dict. Operators -, + and various >/</>=/<= probably are not working. I'm hoping to get feedback on this objective, structure and style before fleshing this out further.

@jayvdb
Copy link
Author

jayvdb commented May 31, 2021

@almarklein , could you approve the workflow for this please.

if (!x) { return s; };
if (typeof x==="object" && !Array.isArray(x)) {x = Object.keys(x)}
for (var i=0; i<x.length; i++) {
if (!s.has(x[i]) && !FUNCTION_PREFIXop_contains(x[i], s)) { s.add(x[i]) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do just s.add here, because JS's set filters duplicates by itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, nested data ...

Comment on lines +443 to +453
C = ast.Set
if (isinstance(node.left_node, C) and
isinstance(node.right_node, C)):
if node.op == node.OPS.BitOr:
return self.use_std_function('op_set_union', [left, right])
if node.op == node.OPS.BitAnd:
return self.use_std_function('op_set_intersection', [left, right])
if node.op == node.OPS.BitXor:
return self.use_std_function('op_set_sym_difference', [left, right])
if node.op == node.OPS.Sub:
return self.use_std_function('op_set_difference', [left, right])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit unsure about this. It allows set operations on literal sets only. The use is rather limited because in most cases you'd just directly write the result. But more importantly, it may fool users into thinking that operators work for sets, which is not the case (except for literals).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to drop these for the moment. They are not in my actual use case. I just included them to outline how they could be implemented on top of the JS Set.

pscript/stdlib.py Outdated Show resolved Hide resolved
pscript/stdlib.py Outdated Show resolved Hide resolved
@almarklein
Copy link
Member

We can add difference, intersection et al. in parser3.py to trigger the functions you added to the stdlib.

@almarklein
Copy link
Member

almarklein commented Jun 7, 2021

Thanks for this! You seem to have found your way around the codebase well, and also added tests. Nice!

This looks good. I added few comments. I think we should drop the operators for literals to avoid confusion, but we could add support for e.g. some_set.difference(). Fine if you want to leave that for another PR/day/person :) We should probably document the limitations for set in the docs too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JavaScript set
2 participants