Spec-Zone.ru › Nushell

chunk-by for filters

Divides a sequence into sub-sequences based on a closure.

Signature

> chunk-by {flags} (closure)

Parameters

  • closure: The closure to run.

Input/output types:

input output
list<any> list<list<any>>
range list<list<any>>

Examples

Chunk data into runs of larger than zero or not.

> [1, 3, -2, -2, 0, 1, 2] | chunk-by {|it| $it >= 0 }
╭───┬────────────╮
│ 0 │ ╭───┬───╮  │
│   │ │ 0 │ 1 │  │
│   │ │ 1 │ 3 │  │
│   │ ╰───┴───╯  │
│ 1 │ ╭───┬────╮ │
│   │ │ 0 │ -2 │ │
│   │ │ 1 │ -2 │ │
│   │ ╰───┴────╯ │
│ 2 │ ╭───┬───╮  │
│   │ │ 0 │ 0 │  │
│   │ │ 1 │ 1 │  │
│   │ │ 2 │ 2 │  │
│   │ ╰───┴───╯  │
╰───┴────────────╯

Identify repetitions in a string

> [a b b c c c] | chunk-by { |it| $it }
╭───┬───────────╮
│ 0 │ ╭───┬───╮ │
│   │ │ 0 │ a │ │
│   │ ╰───┴───╯ │
│ 1 │ ╭───┬───╮ │
│   │ │ 0 │ b │ │
│   │ │ 1 │ b │ │
│   │ ╰───┴───╯ │
│ 2 │ ╭───┬───╮ │
│   │ │ 0 │ c │ │
│   │ │ 1 │ c │ │
│   │ │ 2 │ c │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯

Chunk values of range by predicate

> (0..8) | chunk-by { |it| $it // 3 }
╭───┬───────────╮
│ 0 │ ╭───┬───╮ │
│   │ │ 0 │ 0 │ │
│   │ │ 1 │ 1 │ │
│   │ │ 2 │ 2 │ │
│   │ ╰───┴───╯ │
│ 1 │ ╭───┬───╮ │
│   │ │ 0 │ 3 │ │
│   │ │ 1 │ 4 │ │
│   │ │ 2 │ 5 │ │
│   │ ╰───┴───╯ │
│ 2 │ ╭───┬───╮ │
│   │ │ 0 │ 6 │ │
│   │ │ 1 │ 7 │ │
│   │ │ 2 │ 8 │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯

Notes

chunk-by applies the given closure to each value of the input list, and groups consecutive elements that share the same closure result value into lists.

Copyright © 2019–2026 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/chunk-by.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API