Skip to content

feat(where): Support stored closure - #15697

Merged
Bahex merged 5 commits into
nushell:mainfrom
Bahex:where-closure
May 13, 2025
Merged

Bahex merged 5 commits into
nushell:mainfrom
Bahex:where-closure

Conversation

@Bahex

@Bahex Bahex commented May 5, 2025

Copy link
Copy Markdown
Member

Description

This PR once again allows using where with closures stored in variables.

let cond = { $in mod 2 == 0 }
1..10 | where $cond
# => ╭───┬────╮
# => │ 0 │  2 │
# => │ 1 │  4 │
# => │ 2 │  6 │
# => │ 3 │  8 │
# => │ 4 │ 10 │
# => ╰───┴────╯

let nested = {cond: { $in mod 2 == 0 }}
1..10 | where $nested.cond 
# => ╭───┬────╮
# => │ 0 │  2 │
# => │ 1 │  4 │
# => │ 2 │  6 │
# => │ 3 │  8 │
# => │ 4 │ 10 │
# => ╰───┴────╯

This does not interfere with using $it or one of its fields as the condition.

[[name state]; [foo true] [bar false] [baz true] [quox false]]
| where $it.state
# => ╭───┬──────┬───────╮
# => │ # │ name │ state │
# => ├───┼──────┼───────┤
# => │ 0 │ foo  │ true  │
# => │ 1 │ baz  │ true  │
# => ╰───┴──────┴───────╯

This change obsoletes filter, deprecate it in the future?

User-Facing Changes

Tests + Formatting

Added examples and tests.

  • 🟢 toolkit fmt
  • 🟢 toolkit clippy
  • 🟢 toolkit test
  • 🟢 toolkit test stdlib

After Submitting

@github-actions github-actions Bot added the A:parser Issues related to parsing label May 5, 2025
@Bahex Bahex added the deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way label May 5, 2025
@Bahex
Bahex requested a review from kubouch May 5, 2025 20:16

@132ikl 132ikl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, seems like a very simple change! would like to get @kubouch's sign off on this

Comment thread crates/nu-parser/src/parser.rs Resolved
Comment thread crates/nu-parser/src/parser.rs Resolved
Bahex added 2 commits May 6, 2025 11:27
- Only variables and cell-path accesses on variables are allowed as
  closures. These can't be `$it` or a path on `$it`
- Any other expression falls back to the usual RowCondition expression
Comment on lines +38 to +41
SyntaxShape::OneOf(vec![
SyntaxShape::RowCondition,
SyntaxShape::Closure(Some(vec![SyntaxShape::Any])),
]),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding #16129 ,

Do we need the Closure here if we already manually override the expression type to make it compatible?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blindFS Hey thanks for pointing this out, I just got around to fixing it.

Closure is not actually needed to make it work, just added for documentation.

And removing the it solves the problem without any further work, but I'll try to solve the issue without removing it if I can.

Bahex added a commit that referenced this pull request Jul 22, 2025
…e text (#16192)

- fixes #16129

# Description

## Problem
Parsing a multi span value as RowCondition always produces a RowCondition
expression that covers all the spans.

Which is problematic inside OneOf and can produce multiple expressions with
overlapping spans.
Which results in funky highlighting that duplicates text.

## Solution
Only reason for including `SyntaxShape::Closure` in the signature (#15697) was
for documentation purposes, making it clear in `help` texts that a closure can
be used as the argument.

As our current parser is shape directed, simplifying the command signature
means simplifies the parsing, so using a RowCondition on its own, and instead
making it always look like a union with `closure(any)` solves the issue without
any changes in the parser.

Also, RowCondition always accepts closure values anyway, so its textual
representation should indicate that without the need to wrap it in OneOf.

Co-authored-by: Bahex <[email protected]>
hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Aug 25, 2025
…e text (nushell#16192)

- fixes nushell#16129

# Description

## Problem
Parsing a multi span value as RowCondition always produces a RowCondition
expression that covers all the spans.

Which is problematic inside OneOf and can produce multiple expressions with
overlapping spans.
Which results in funky highlighting that duplicates text.

## Solution
Only reason for including `SyntaxShape::Closure` in the signature (nushell#15697) was
for documentation purposes, making it clear in `help` texts that a closure can
be used as the argument.

As our current parser is shape directed, simplifying the command signature
means simplifies the parsing, so using a RowCondition on its own, and instead
making it always look like a union with `closure(any)` solves the issue without
any changes in the parser.

Also, RowCondition always accepts closure values anyway, so its textual
representation should indicate that without the need to wrap it in OneOf.

Co-authored-by: Bahex <[email protected]>
@Bahex
Bahex deleted the where-closure branch March 22, 2026 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:parser Issues related to parsing deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants