Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upAdded `∑`/`sum` support #126
Conversation
bvssvni
merged commit 8fa41e3
into
PistonDevelopers:master
May 7, 2016
bvssvni
deleted the
bvssvni:sum
branch
May 7, 2016
This comment has been minimized.
This comment has been minimized.
TheNeikos
commented
May 8, 2016
•
|
I gotta say I love this concept |
This comment has been minimized.
This comment has been minimized.
dobkeratops
commented
May 17, 2016
•
|
No idea how this language engine works, but do you think you could go as far as inferring the range from the context in the expression. e.g. "sum(i) {foo[i]*bar[i]} " would infer the range of 'i' from its use indexing foo, bar. (whatever syntax makes sense). |
This comment has been minimized.
This comment has been minimized.
|
@dobkeratops I opened #181 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
bvssvni commentedMay 7, 2016
•
edited
See #119
This adds a
∑loop (both∑andsumcan be used) which initializes a number to 0 and sums up the result from the body, over an index.Example:
This is logically equal to:
Just like a
forloop, you can usecontinueandbreak. When usingcontinue, it jumps right to the next index without adding the numbers.I was thinking about adding just the
∑loop and play with it for a while, before deciding whether to add more variants.Motivation
Dyon has a very simple object model, so there are no iterators like in Rust. It is also an open question whether closures can be made completely safe without a statically type system. To make up for this, we could explore some other syntax.
The benefits of such loops are:
Performance
Before:
After:
It is 3x faster than the short For loop, and 9x faster than the traditional For loop.
Under the hood
The AST uses
ForNto describe both the short For loop and the new ∑ loop. The meta syntax is almost identical, and might be simplified further. The only difference is that it stores it in a differentExpression::Sum(Box<ForN>)variant, instead ofExpression::ForN(Box<ForN>).Other ideas
An idea I had was to support both
i := 0; i < 10; i += 1pattern and short version for all loops.However, I there are several down sides to support the traditional syntax for such loop:
Expressionvariant to avoid overheadAnother idea was for optimization. If you write a ∑ loop with a constant end and this is a small number, it is very easy to replace the AST node with an unrolled loop that performs the addition straight forward. However, it needs to check that
continueorbreakis not used within the body, and that the counter is not mutated.