take while

For filters.

Take elements of the input while a predicate is true.

Command Type

built-in

Signature

> take while {flags} (predicate)

Flags

  • --include, -i {int}: Include extra items after the stream would otherwise have stopped. 0 is a no-op.

Parameters

  • predicate: The predicate that element(s) must match.

Input/output types:

input output
table table
list<any> list<any>

Examples

Take while the element is negative.

> [-1 -2 9 1] | take while {|x| $x < 0 }
╭───┬────╮
│ 0 │ -1 │
│ 1 │ -2 │
╰───┴────╯

Take while the element is negative using stored condition.

> let cond = {|x| $x < 0 }; [-1 -2 9 1] | take while $cond
╭───┬────╮
│ 0 │ -1 │
│ 1 │ -2 │
╰───┴────╯

Take while the field value is negative.

> [{a: -1} {a: -2} {a: 9} {a: 1}] | take while {|x| $x.a < 0 }
╭───┬────╮
│ # │ a  │
├───┼────┤
│ 0 │ -1 │
│ 1 │ -2 │
╰───┴────╯

Take until the first item without a lowercase name including that item.

> [[name value]; [b, 2], [c, 3], [A, 1], [D, 4]] | take while -i 1 {|x| $x.name like '[a-z]' }
╭───┬──────┬───────╮
│ # │ name │ value │
├───┼──────┼───────┤
│ 0 │ b    │     2 │
│ 1 │ c    │     3 │
│ 2 │ A    │     1 │
╰───┴──────┴───────╯

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