Updated Loop statement expression (markdown)
@@ -9,7 +9,7 @@ The simplest loop consists only of counter, loop operator and instruction or blo
|
||||
of instructions.
|
||||
|
||||
Example:
|
||||
```
|
||||
```php
|
||||
# the simplest loop
|
||||
3 ^ print("Money "); # Money Money Money
|
||||
|
||||
@@ -23,7 +23,7 @@ Example:
|
||||
```
|
||||
|
||||
Of course, you can use any expression as counter, like variable or function call:
|
||||
```
|
||||
```php
|
||||
function provideCounter(x: int) {
|
||||
return x * 2;
|
||||
}
|
||||
@@ -33,7 +33,7 @@ provideCounter(5) ^ print("a"); # aaaaaaaaaa
|
||||
|
||||
You can define a counter variable whose value is increased with each iteration
|
||||
using `as` clause (note, that values start from 0):
|
||||
```
|
||||
```php
|
||||
3 as i ^ print(i, " "); # 0 1 2
|
||||
```
|
||||
|
||||
@@ -42,7 +42,7 @@ You can create a loop which is controlled by logic condition.
|
||||
It can be done just putting a `bool` value as a counter.
|
||||
|
||||
Example:
|
||||
```
|
||||
```php
|
||||
end = false;
|
||||
i = 0;
|
||||
|
||||
@@ -69,7 +69,7 @@ for each element of list. You can obtain access to element through `as` clause
|
||||
like in previous examples.
|
||||
|
||||
Example:
|
||||
```
|
||||
```php
|
||||
myList = [@c, @d, @e];
|
||||
|
||||
myList ^ print("Money "); # Money Money Money
|
||||
@@ -82,7 +82,7 @@ will be the value of passed list and the second one will be iterator that is inc
|
||||
you have to enclose them between parentheses.
|
||||
|
||||
Example:
|
||||
```
|
||||
```php
|
||||
[@c, @d, @e] as (n, i) ^ println(i, ". ", n);
|
||||
|
||||
# Output:
|
||||
@@ -96,7 +96,7 @@ Map is also supported to act as a counter of loop.
|
||||
In this case statement will be executed for each value of passed map.
|
||||
|
||||
Example:
|
||||
```
|
||||
```php
|
||||
x = {
|
||||
a -> @c,
|
||||
b -> @d,
|
||||
@@ -113,7 +113,7 @@ You can use up to three parameters, where
|
||||
* the third is an iterator increased by one with each iteration.
|
||||
|
||||
Example:
|
||||
```
|
||||
```php
|
||||
myMap = {
|
||||
first -> true,
|
||||
second -> [@c, @d, @e],
|
||||
@@ -144,7 +144,7 @@ Loop operator supports also `string` counter. In this case, you are able to iter
|
||||
of passed string. As in the previous cases, you are able to access both current character and iterator using `as` clause.
|
||||
|
||||
Example:
|
||||
```
|
||||
```php
|
||||
text = "Hello";
|
||||
|
||||
text ^ print("0"); # 00000
|
||||
@@ -171,7 +171,7 @@ that must be met to execute iteration. `%` clause is placed at the very last
|
||||
of loop statement.
|
||||
|
||||
Example:
|
||||
```
|
||||
```php
|
||||
10 as i ^ println(i) % mod(i, 2) == 0;
|
||||
# Output:
|
||||
# 0
|
||||
@@ -194,7 +194,7 @@ expression as a loop's statement. Because of that in loop expression you can't u
|
||||
block of code. The statement must be a single instruction - expression.
|
||||
|
||||
Example:
|
||||
```
|
||||
```php
|
||||
x = [1, 2, 3, 4];
|
||||
|
||||
# 'y' holds numbers multipied by 2
|
||||
@@ -207,7 +207,7 @@ z = x as i ^ i ** 2 % mod(i, 2) == 0; # y = [4.0, 16.0]
|
||||
Note, that loop statement is right associative.
|
||||
That means nested loop statements are being accumulated in right-hand branch.
|
||||
Take look at example:
|
||||
```
|
||||
```php
|
||||
2 ^ 4 ^ 6 ^ print("a");
|
||||
# is exactly the same as
|
||||
2 ^ (4 ^ (6 ^ print("a")));
|
||||
@@ -226,7 +226,7 @@ It could be a flaw if you would like to create a mapping chain using loop statem
|
||||
(like streams in Java 8 or LINQ in .NET), because it requires from operator
|
||||
to be left associative, so that first instruction would produce value and pass it further.
|
||||
It can be of course achieved with parentheses:
|
||||
```
|
||||
```php
|
||||
data = ["lorem", "ipsum", "dolor", "sit", "amet"];
|
||||
output = (((((data as d
|
||||
^ d
|
||||
|
||||
Reference in New Issue
Block a user