par-each

For filters.

Run a closure on each row of the input list in parallel, creating a new list with the results.

Command Type

built-in

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
list<any> list<any>
table list<any>
any 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! │
╰───┴───────────────╯

Notes

Uses a dedicated thread pool (reused across top-level calls; sized by —threads when set). Nested par-each calls use a private pool so they cannot deadlock on the outer pool.

Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.