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

∑/sum, ∏/prod, min, max, sift, ∃/any, ∀/all loops #119

Closed
bvssvni opened this issue May 7, 2016 · 0 comments
Closed

∑/sum, ∏/prod, min, max, sift, ∃/any, ∀/all loops #119

bvssvni opened this issue May 7, 2016 · 0 comments

Comments

@bvssvni
Copy link
Member

bvssvni commented May 7, 2016

In mathematics, you have the ∑ symbol that appears all over the place. It can be combined with a body with an arbitrary mathematical expression. Dyon uses this concept, making it easier to write programs in a more mathematical style.

The unicode symbol is be allowed in the syntax instead of the words "sum".

Example:

a := ∑ i len(list) { list[i] }

vs

sum := 0
for i len(list) {
    sum += list[i]
}

It can be used within an expression:

n := len(list)
mean := ∑ i n { list[i] } / n
geometric_mean := exp(∑ i n { ln(list[i]) } / n)

You can use continue and break, just like a normal for loop:

  • continue skips the current index
  • break skips the rest of the indices

Other loops:

  • ∏/prod (starts with 1)
  • min (starts with none())
  • max (starts with none())
  • sift (starts with [], puts result of block into an array)
  • ∃/any (returns bool, starts with false)
  • ∀/all (returns bool, starts with true)

Composition of loops

All the loops described here can be composed, which means to nest them and return a value from the inner loop:

a := min i {
        min j { ... }
    }

This can be written as a packed loop:

a := min i, j { ... }

Secrets

any, all, min and max loops, and their compositions, generate a secret. This is used to find out why or where a value was returned from the loop:

a := min i, j { ... }
println(where(a)) // prints `[i, j]` of minimum value.

For more information about secrets, see #266

bvssvni added a commit to bvssvni/dyon that referenced this issue May 9, 2016
@bvssvni bvssvni added information and removed hard labels May 16, 2016
@bvssvni bvssvni changed the title sum and prod loops ∑/sum, min, max, sift, ∃/any, ∀/all loops May 16, 2016
@bvssvni bvssvni changed the title ∑/sum, min, max, sift, ∃/any, ∀/all loops ∑/sum, ∏/prod, min, max, sift, ∃/any, ∀/all loops Jul 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant