Updated Loop statement expression (markdown)
@@ -9,7 +9,7 @@ The simplest loop consists only of counter, loop operator and instruction or blo
|
|||||||
of instructions.
|
of instructions.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```php
|
||||||
# the simplest loop
|
# the simplest loop
|
||||||
3 ^ print("Money "); # Money Money Money
|
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:
|
Of course, you can use any expression as counter, like variable or function call:
|
||||||
```
|
```php
|
||||||
function provideCounter(x: int) {
|
function provideCounter(x: int) {
|
||||||
return x * 2;
|
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
|
You can define a counter variable whose value is increased with each iteration
|
||||||
using `as` clause (note, that values start from 0):
|
using `as` clause (note, that values start from 0):
|
||||||
```
|
```php
|
||||||
3 as i ^ print(i, " "); # 0 1 2
|
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.
|
It can be done just putting a `bool` value as a counter.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```php
|
||||||
end = false;
|
end = false;
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ for each element of list. You can obtain access to element through `as` clause
|
|||||||
like in previous examples.
|
like in previous examples.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```php
|
||||||
myList = [@c, @d, @e];
|
myList = [@c, @d, @e];
|
||||||
|
|
||||||
myList ^ print("Money "); # Money Money Money
|
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.
|
you have to enclose them between parentheses.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```php
|
||||||
[@c, @d, @e] as (n, i) ^ println(i, ". ", n);
|
[@c, @d, @e] as (n, i) ^ println(i, ". ", n);
|
||||||
|
|
||||||
# Output:
|
# 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.
|
In this case statement will be executed for each value of passed map.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```php
|
||||||
x = {
|
x = {
|
||||||
a -> @c,
|
a -> @c,
|
||||||
b -> @d,
|
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.
|
* the third is an iterator increased by one with each iteration.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```php
|
||||||
myMap = {
|
myMap = {
|
||||||
first -> true,
|
first -> true,
|
||||||
second -> [@c, @d, @e],
|
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.
|
of passed string. As in the previous cases, you are able to access both current character and iterator using `as` clause.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```php
|
||||||
text = "Hello";
|
text = "Hello";
|
||||||
|
|
||||||
text ^ print("0"); # 00000
|
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.
|
of loop statement.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```php
|
||||||
10 as i ^ println(i) % mod(i, 2) == 0;
|
10 as i ^ println(i) % mod(i, 2) == 0;
|
||||||
# Output:
|
# Output:
|
||||||
# 0
|
# 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.
|
block of code. The statement must be a single instruction - expression.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```php
|
||||||
x = [1, 2, 3, 4];
|
x = [1, 2, 3, 4];
|
||||||
|
|
||||||
# 'y' holds numbers multipied by 2
|
# '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.
|
Note, that loop statement is right associative.
|
||||||
That means nested loop statements are being accumulated in right-hand branch.
|
That means nested loop statements are being accumulated in right-hand branch.
|
||||||
Take look at example:
|
Take look at example:
|
||||||
```
|
```php
|
||||||
2 ^ 4 ^ 6 ^ print("a");
|
2 ^ 4 ^ 6 ^ print("a");
|
||||||
# is exactly the same as
|
# is exactly the same as
|
||||||
2 ^ (4 ^ (6 ^ print("a")));
|
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
|
(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.
|
to be left associative, so that first instruction would produce value and pass it further.
|
||||||
It can be of course achieved with parentheses:
|
It can be of course achieved with parentheses:
|
||||||
```
|
```php
|
||||||
data = ["lorem", "ipsum", "dolor", "sit", "amet"];
|
data = ["lorem", "ipsum", "dolor", "sit", "amet"];
|
||||||
output = (((((data as d
|
output = (((((data as d
|
||||||
^ d
|
^ d
|
||||||
|
|||||||
Reference in New Issue
Block a user