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:
aquaxs: []stringfor x <- xs:y <- foo(x)-- x and y are not accessible there, you can even redefine themx <- bar()y <- baz()
aquaxs: []stringfor x <- xs:y <- foo(x)-- x and y are not accessible there, you can even redefine themx <- bar()y <- baz()
Contract
- Iterations of
forloop are executed sequentially by default. - Variables defined inside
forloop are not available outside. forloop's code has access to all variables above.forcan 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.
aquaxs: []stringfor x <- xs try:-- Will stop trying once foo succeedsfoo(x)
aquaxs: []stringfor x <- xs try:-- Will stop trying once foo succeedsfoo(x)
The contract is changed as in Parallel flow.
Parallel for
Running many operations in parallel is the most commonly used pattern for for.
aquaxs: []stringfor x <- xs par:on x:foo()-- Once the fastest x succeeds, execution continues-- If you want to make the subsequent execution independent from for,-- mark it with par, e.g.:par continueWithBaz()
aquaxs: []stringfor x <- xs par:on x:foo()-- Once the fastest x succeeds, execution continues-- If you want to make the subsequent execution independent from for,-- mark it with par, e.g.:par continueWithBaz()
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.
aquaxs: []stringreturn: *string-- can be par, try, or nothingfor x <- xs par:on x:return <- foo()-- Wait for 6 fastest results -- see Join behaviorbaz(return!5, return)
aquaxs: []stringreturn: *string-- can be par, try, or nothingfor x <- xs par:on x:return <- foo()-- Wait for 6 fastest results -- see Join behaviorbaz(return!5, return)
for on streams
for on streams is one of the most advanced and powerful parts of Aqua. See CRDT streams for details.