github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/structs/for_doc.yaml (about)

     1  - DocumentID: for
     2    Title: >+
     3      `for`
     4    CategoryID: commands
     5    Summary: >-
     6      A more familiar iteration loop to existing developers
     7    Description: |-
     8      This `for` loop is fills a small niche where `foreach` or `formap` are
     9      inappropiate in your script. It's generally not recommended to use `for`
    10      because it performs slower and doesn't adhere to Murex's design
    11      philosophy. However it does offer additional flexibility around recursion.
    12    Usage: |-
    13      ```
    14      for ( variable; conditional; incrementation ) { code-block } -> <stdout>
    15      ```
    16    Examples: |-
    17      ```
    18      » for ( i=1; i<6; i++ ) { echo $i }
    19      1
    20      2
    21      3
    22      4
    23      5
    24      ```
    25    Flags:
    26    Detail: |-
    27      ### Syntax
    28  
    29      `for` is a little naughty in terms of breaking Murex's style guidelines due
    30      to the first parameter being entered as one string treated as 3 separate code
    31      blocks. The syntax is like this for two reasons:
    32        
    33      1. readability (having multiple `{ blocks }` would make scripts unsightly
    34      2. familiarity (for those using to `for` loops in other languages
    35  
    36      The first parameter is: `( i=1; i<6; i++ )`, but it is then converted into the
    37      following code:
    38  
    39      1. `let i=0` - declare the loop iteration variable
    40      2. `= i<0` - if the condition is true then proceed to run the code in
    41      the second parameter - `{ echo $i }`
    42      3. `let i++` - increment the loop iteration variable
    43  
    44      The second parameter is the code to execute upon each iteration
    45  
    46      ### Better `for` loops
    47  
    48      Because each iteration of a `for` loop reruns the 2nd 2 parts in the first
    49      parameter (the conditional and incrementation), `for` is very slow. Plus the
    50      weird, non-idiomatic, way of writing the 3 parts, it's fair to say `for` is
    51      not the recommended method of iteration and in fact there are better functions
    52      to achieve the same thing...most of the time at least.
    53  
    54      For example:
    55  
    56      ```
    57      a [1..5] -> foreach i { echo $i }
    58      1
    59      2
    60      3
    61      4
    62      5
    63      ```
    64  
    65      The different in performance can be measured. eg:
    66  
    67      ```
    68      » time { a [1..9999] -> foreach i { out <null> $i } }
    69      0.097643108
    70  
    71      » time { for ( i=1; i<10000; i=i+1 ) { out <null> $i } }
    72      0.663812496
    73      ```
    74  
    75      You can also do step ranges with `foreach`:
    76  
    77      ```
    78      » time { for ( i=10; i<10001; i=i+2 ) { out <null> $i } }
    79      0.346254973
    80  
    81      » time { a [1..999][0,2,4,6,8],10000 -> foreach i { out <null> $i } }
    82      0.053924326
    83      ```
    84  
    85      ...though granted the latter is a little less readable.
    86  
    87      The big catch with using `a` piped into `foreach` is that values are passed
    88      as strings rather than numbers.
    89  
    90      {{ include "gen/includes/for-loop-json-tips.inc.md" }}
    91    Synonyms:
    92    Related:
    93      - formap
    94      - foreach
    95      - while
    96      - if
    97      - let
    98      - set
    99      - a
   100      - ja
   101      - break