Skip to content

ForEach

Name Mandatory Description Default Type
⬅️ Input Sequence/table whose elements or key-value pairs need to be processed. [Any]{Any}
Output ➡️ Outputs the input value, passed through unchanged. [Any]{Any}
Apply No The processing logic (in the form of a shard or sequence of shards) to apply to the input sequence/table. none Shard[Shard]

Processes every element or key-value pair of a sequence/table with the shards specified in the Apply parameter. Note that this shard is able to use the $0 and $1 internal variables, as well as $i for the current index.

Details

This shard is able to use the internal variable $0 and $1 within its Apply parameter.

If a sequence was provided as input, the variable $0 will take the value of the current element of the sequence it is iterating over. ($1 does not exist when iterating over a sequence) For example:

[1 2 3]
ForEach(
  Apply: {
    $0 | Math.Add(1)
  }
)
$0 will take the value of 1 and then 2 and then 3 accordingly.

When iterating over a sequence with elements of different types, $0 will adopt the type of the current element it is iterating over, convert its type as necessary.

If a table was provided as input, the variable $0 will be the current key of the table it is iterating over while $1 will take the value associated with the key. For example:

{
  a: 1
  b: 2
  c: 3
}
ForEach(
  Apply: {
    $0 | LogType("Type") | Log("key")
    $1 | ExpectInt | Math.Add(1)
  }
)
$0 will take the value of "a" and then "b" and then "c" while $1 will take the value of 1 and then 2 and then 3 accordingly.

When iterating over a table, the 'Apply' parameter receives a sequence of two elements as input, where the first element is the key and the second is the value. $0 and $1 are set to these values respectively.

$0 will always be of type String while $1, when iterating over the table will always be of type Any. Convert $1 to the appropriate type as necessary (e.g. using ExpectInt).

Do note that this instance of $0 and $1 are unique to the ForEach shard and do not exist outside of the context of its Apply parameter. Values set to this instance of $0 and $1 will not be reflected on other $0 and $1 created in a different call of another ForEach shard or any other shard that is also able to use $0 and $1.

ForEach returns the input unchanged unlike Map which returns the modified sequence or table.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
; ForEach on a sequence: processes every element in seq order
[2 4 8 10] | ForEach({
  {Math.Multiply(10) | Log}  ; => 20, 40, 80, 100
  {Math.Multiply(100) | Log} ; => 200, 400, 800, 1000
})

; ForEach on a table: processes every key in alphabetic order
{Name: "Universe" Age: "13.8B Yrs"} | ForEach({
  ; receives each key/value pair as a sequence in alphabetic order of key
  {Slice(0 1 1) | Log}  ; => ["Age"], ["Name"]
  {Slice(1 2 1) | Log}  ; => ["13.8 B Yrs"], ["Universe"]
})

[info] [sample-wire] 20
[info] [sample-wire] 200
[info] [sample-wire] 40
[info] [sample-wire] 400
[info] [sample-wire] 80
[info] [sample-wire] 800
[info] [sample-wire] 100
[info] [sample-wire] 1000
[info] [sample-wire] [Age]
[info] [sample-wire] [13.8B Yrs]
[info] [sample-wire] [Name]
[info] [sample-wire] [Universe]