W3cubDocs

/Nushell

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
list<any> list<any>
range list<any>
table list<any>

Examples

Multiplies each number. Note that the list will become arbitrarily disordered.

> [123] |par-each {|e| $e*2 }

Multiplies each number, keeping an original order

> [123] |par-each--keep-order {|e| $e*2 }
╭───┬───╮
02
14
26
╰───┴───╯

Enumerate and sort-by can be used to reconstruct the original order

>1..3|enumerate|par-each {|p| updateitem ($p.item*2)} |sort-byitem|getitem
╭───┬───╮
02
14
26
╰───┴───╯

Output can still be sorted afterward

> [foobarbaz] |par-each {|e| $e+'!' } |sort
╭───┬──────╮
0bar!
1baz!
2foo!
╰───┴──────╯

Iterate over each element, producing a list showing indexes of any 2s

> [123] |enumerate|par-each { |e| if$e.item==2 { $"found 2 at ($e.index)!"} }
╭───┬───────────────╮
0found2at1!
╰───┴───────────────╯

Copyright © 2019–2023 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/par-each.html