Skip to content

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

@bvssvni

Description

@bvssvni

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions