Iterative
Ο€-calculus has a notion of the repetitive process: !P = P | !P. That means, you can always fork a new P process if you need it.
In Aqua, two operations correspond to it: you can call a service function (it's just available when it's needed), and you can use for loop to iterate on collections.

for expression

In short, for looks like the following:
1
xs: []string
2
​
3
for x <- xs:
4
y <- foo(x)
5
​
6
-- x and y are not accessible there, you can even redefine them
7
x <- bar()
8
y <- baz()
Copied!

Contract

  • Iterations of for loop are executed sequentially by default.
  • Variables defined inside for loop are not available outside.
  • for loop's code has access to all variables above.
  • for can be executed on a variable of any Collection type.

Conditional for

For can be executed on a variable of any Collection type. You can make several trials in a loop, and break once any trial succeeded.
1
xs: []string
2
​
3
for x <- xs try:
4
-- Will stop trying once foo succeeds
5
foo(x)
Copied!
The contract is changed as in Parallel flow.

Parallel for

Running many operations in parallel is the most commonly used pattern for for.
1
xs: []string
2
​
3
for x <- xs par:
4
on x:
5
foo()
6
​
7
-- Once the fastest x succeeds, execution continues
8
-- If you want to make the subsequent execution independent from for,
9
-- mark it with par, e.g.:
10
par continueWithBaz()
Copied!
The contract is changed as in Conditional flow.

Export data from for

The way to export data from for is the same as in Conditional return and Race patterns.
1
xs: []string
2
return: *string
3
​
4
-- can be par, try, or nothing
5
for x <- xs par:
6
on x:
7
return <- foo()
8
​
9
-- Wait for 6 fastest results -- see Join behavior
10
baz(return!5, return)
Copied!

for on streams

for on streams is one of the most advanced and powerful parts of Aqua. See CRDT streams for details.