for
A more familiar iteration loop to existing developers
This for
loop is fills a small niche where foreach
or formap
idioms will fail within your scripts. It's generally not recommended to use for
because it performs slower and doesn't adhere to murex's design philosophy.
for ( variable; conditional; incrementation ) { code-block } -> <stdout>
» for ( i=1; i<6; i++ ) { echo $i }
1
2
3
4
5
for
is a little naughty in terms of breaking murex's style guidelines due to the first parameter being entered as one string treated as 3 separate code blocks. The syntax is like this for two reasons:
{ blocks }
would make scripts unsightlyfor
loops in other languagesThe first parameter is: ( i=1; i<6; i++ )
, but it is then converted into the following code:
let i=0
- declare the loop iteration variable= i<0
- if the condition is true then proceed to run the code in the second parameter - { echo $i }
let i++
- increment the loop iteration variableThe second parameter is the code to execute upon each iteration
for
loopsBecause each iteration of a for
loop reruns the 2nd 2 parts in the first parameter (the conditional and incrementation), for
is very slow. Plus the weird, non-idiomatic, way of writing the 3 parts, it's fair to say for
is not the recommended method of iteration and in fact there are better functions to achieve the same thing...most of the time at least.
For example:
a: [1..5] -> foreach: i { echo $i }
1
2
3
4
5
The different in performance can be measured. eg:
» time { a: [1..9999] -> foreach: i { out: <null> $i } }
0.097643108
» time { for ( i=1; i<10000; i=i+1 ) { out: <null> $i } }
0.663812496
You can also do step ranges with foreach
:
» time { for ( i=10; i<10001; i=i+2 ) { out: <null> $i } }
0.346254973
» time { a: [1..999][0,2,4,6,8],10000 -> foreach i { out: <null> $i } }
0.053924326
...though granted the latter is a little less readable.
One of the drawbacks (or maybe advantages, depending on your perspective) of JSON is that parsers generally expect a complete file for processing in that the JSON specification requires closing tags for every opening tag. This means it's not always suitable for streaming. For example
» ja [1..3] -> foreach i { out ({ "$i": $i }) }
{ "1": 1 }
{ "2": 2 }
{ "3": 3 }
What does this even mean and how can you build a JSON file up sequentially?
One answer if to write the output in a streaming file format and convert back to JSON
» ja [1..3] -> foreach i { out (- "$i": $i) }
- "1": 1
- "2": 2
- "3": 3
» ja [1..3] -> foreach i { out (- "$i": $i) } -> cast yaml -> format json
[
{
"1": 1
},
{
"2": 2
},
{
"3": 3
}
]
What if I'm returning an object rather than writing one?
The problem with building JSON structures from existing structures is that you can quickly end up with invalid JSON due to the specifications strict use of commas.
» config -> [ shell ] -> formap k v { $v -> alter /Foo Bar }
{
"Data-Type": "bool",
"Default": true,
"Description": "Display the interactive shell's hint text helper. Please note, even when this is disabled, it will still appear when used for regexp searches and other readline-specific functions",
"Dynamic": false,
"Foo": "Bar",
"Global": true,
"Value": true
}
{
"Data-Type": "block",
"Default": "{ progress $PID }",
"Description": "Murex function to execute when an `exec` process is stopped",
"Dynamic": false,
"Foo": "Bar",
"Global": true,
"Value": "{ progress $PID }"
}
{
"Data-Type": "bool",
"Default": true,
"Description": "ANSI escape sequences in Murex builtins to highlight syntax errors, history completions, {SGR} variables, etc",
"Dynamic": false,
"Foo": "Bar",
"Global": true,
"Value": true
}
...
Luckily JSON also has it's own streaming format: JSON lines (jsonl
)
» config -> [ shell ] -> formap k v { $v -> alter /Foo Bar } -> cast jsonl -> format json
[
{
"Data-Type": "bool",
"Default": true,
"Description": "Write shell history (interactive shell) to disk",
"Dynamic": false,
"Foo": "Bar",
"Global": true,
"Value": true
},
{
"Data-Type": "int",
"Default": 4,
"Description": "Maximum number of lines with auto-completion suggestions to display",
"Dynamic": false,
"Foo": "Bar",
"Global": true,
"Value": "6"
},
{
"Data-Type": "bool",
"Default": true,
"Description": "Display some status information about the stop process when ctrl+z is pressed (conceptually similar to ctrl+t / SIGINFO on some BSDs)",
"Dynamic": false,
"Foo": "Bar",
"Global": true,
"Value": true
},
...
foreach
will automatically cast it's output as jsonl
if it's STDIN type is json
» ja: [Tom,Dick,Sally] -> foreach: name { out Hello $name }
Hello Tom
Hello Dick
Hello Sally
» ja [Tom,Dick,Sally] -> foreach name { out Hello $name } -> debug -> [[ /Data-Type/Murex ]]
jsonl
» ja: [Tom,Dick,Sally] -> foreach: name { out Hello $name } -> format: json
[
"Hello Tom",
"Hello Dick",
"Hello Sally"
]
a
(mkarray): A sophisticated yet simple way to build an array or listforeach
: Iterate through an arrayformap
: Iterate through a map or other collection of dataif
: Conditional statement to execute different blocks of code depending on the result of the conditionja
(mkarray): A sophisticated yet simply way to build a JSON arraylet
: Evaluate a mathematical function and assign to variableset
: Define a local variable and set it's valuewhile
: Loop until condition falseThis site's content is rebuilt automatically from murex's source code after each merge to the master
branch. Downloadable murex binaries are also built with the website.
Last built on Mon May 23 22:12:14 UTC 2022 against commit b81828ab81828a7ff621aa4b9554e6509f225a315a126fe.
Current version is 2.8.2100 BETA which has been verified against 15889 tests.