par-each
for filters
Run a closure on each row of the input list in parallel, creating a new list with the results.
Signature
> par-each {flags} (closure)
Flags
--threads, -t {int}
: the number of threads to use--keep-order, -k
: keep sequence of output same as the order of input
Parameters
closure
: The closure to run.
Input/output types:
input | output |
---|---|
any | any |
list<any> | list<any> |
table | list<any> |
Examples
Multiplies each number. Note that the list will become arbitrarily disordered.
> [1 2 3] | par-each {|e| $e * 2 }
Multiplies each number, keeping an original order
> [1 2 3] | par-each --keep-order {|e| $e * 2 }
╭───┬───╮
│ 0 │ 2 │
│ 1 │ 4 │
│ 2 │ 6 │
╰───┴───╯
Enumerate and sort-by can be used to reconstruct the original order
> 1..3 | enumerate | par-each {|p| update item ($p.item * 2)} | sort-by item | get item
╭───┬───╮
│ 0 │ 2 │
│ 1 │ 4 │
│ 2 │ 6 │
╰───┴───╯
Output can still be sorted afterward
> [foo bar baz] | par-each {|e| $e + '!' } | sort
╭───┬──────╮
│ 0 │ bar! │
│ 1 │ baz! │
│ 2 │ foo! │
╰───┴──────╯
Iterate over each element, producing a list showing indexes of any 2s
> [1 2 3] | enumerate | par-each { |e| if $e.item == 2 { $"found 2 at ($e.index)!"} }
╭───┬───────────────╮
│ 0 │ found 2 at 1! │
╰───┴───────────────╯