added scarpet c_for loops
This commit is contained in:
1 parent
9da1f087fd
commit
3adc628e8f
3 files changed
+54
No files matched your search
@@ -985,6 +985,20 @@ Please check corresponding loop function description what `continue` and `break`
|
|||||||
general case, passed values to `break` and `continue` will be used in place of the return value of the internal
|
general case, passed values to `break` and `continue` will be used in place of the return value of the internal
|
||||||
iteration expression.
|
iteration expression.
|
||||||
|
|
||||||
|
### `cfor(init, condition, increment, body)`
|
||||||
|
|
||||||
|
`c_for` Mimics c-style tri-arg (plus body) for loops. Return value of `c_for` is number of iterations performed in the
|
||||||
|
loop. Unlike other loops, the `body` is not provided with pre-initialized `_` style variables - all initialization
|
||||||
|
and increments has to be handled by the programmers themselves.
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
c_for(x=0, x<10, x+=1,
|
||||||
|
c_for(y=0, y<10, y+=1,
|
||||||
|
print(str('%d * %d = %d', x, y, x*y))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
</pre>
|
||||||
|
|
||||||
### `for(list,expr(_,_i))`
|
### `for(list,expr(_,_i))`
|
||||||
|
|
||||||
Evaluates expression over list of items from the `list`. Supplies `_`(value) and `_i`(iteration number) to the `expr`.
|
Evaluates expression over list of items from the `list`. Supplies `_`(value) and `_i`(iteration number) to the `expr`.
|
||||||
|
|||||||
@@ -19,6 +19,20 @@ Please check corresponding loop function description what `continue` and `break`
|
|||||||
general case, passed values to `break` and `continue` will be used in place of the return value of the internal
|
general case, passed values to `break` and `continue` will be used in place of the return value of the internal
|
||||||
iteration expression.
|
iteration expression.
|
||||||
|
|
||||||
|
### `cfor(init, condition, increment, body)`
|
||||||
|
|
||||||
|
`c_for` Mimics c-style tri-arg (plus body) for loops. Return value of `c_for` is number of iterations performed in the
|
||||||
|
loop. Unlike other loops, the `body` is not provided with pre-initialized `_` style variables - all initialization
|
||||||
|
and increments has to be handled by the programmers themselves.
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
c_for(x=0, x<10, x+=1,
|
||||||
|
c_for(y=0, y<10, y+=1,
|
||||||
|
print(str('%d * %d = %d', x, y, x*y))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
</pre>
|
||||||
|
|
||||||
### `for(list,expr(_,_i))`
|
### `for(list,expr(_,_i))`
|
||||||
|
|
||||||
Evaluates expression over list of items from the `list`. Supplies `_`(value) and `_i`(iteration number) to the `expr`.
|
Evaluates expression over list of items from the `list`. Supplies `_`(value) and `_i`(iteration number) to the `expr`.
|
||||||
|
|||||||
@@ -1081,6 +1081,32 @@ public class Expression
|
|||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// runs traditional for(init, condition, increment, body) tri-argument for loop with body in between
|
||||||
|
addLazyFunction("c_for", 4, (c, t, lv) -> {
|
||||||
|
LazyValue initial = lv.get(0);
|
||||||
|
LazyValue condition = lv.get(1);
|
||||||
|
LazyValue increment = lv.get(2);
|
||||||
|
LazyValue body = lv.get(3);
|
||||||
|
int iterations = 0;
|
||||||
|
for (initial.evalValue(c, Context.VOID); condition.evalValue(c, Context.BOOLEAN).getBoolean(); increment.evalValue(c, Context.VOID))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
body.evalValue(c, Context.VOID);
|
||||||
|
}
|
||||||
|
catch (BreakStatement stmt)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (ContinueStatement ignored)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
iterations++;
|
||||||
|
}
|
||||||
|
int finalIterations = iterations;
|
||||||
|
return (cc, tt) -> new NumericValue(finalIterations);
|
||||||
|
});
|
||||||
|
|
||||||
// similar to map, but returns total number of successes
|
// similar to map, but returns total number of successes
|
||||||
// for(list, expr) => success_count
|
// for(list, expr) => success_count
|
||||||
// can be substituted for first and all, but first is more efficient and all doesn't require knowing list size
|
// can be substituted for first and all, but first is more efficient and all doesn't require knowing list size
|
||||||
|
|||||||
Reference in new issue
Block a user