Skip to content

Move 'where' to parser keywords; Add 'filter' command - #7365

Merged
kubouch merged 10 commits into
nushell:mainfrom
kubouch:where-parser
Dec 10, 2022
Merged

kubouch merged 10 commits into
nushell:mainfrom
kubouch:where-parser

Conversation

@kubouch

@kubouch kubouch commented Dec 6, 2022 •

Copy link
Copy Markdown
Contributor

Description

This PR moves the where command to a parser keyword. While it still uses the shape-directed parsing dictated by the signature, we're free to change the parsing code now to a custom one once we remove the syntax shapes.

As a side effect, the where -b flag was removed and its functionality has moved to the new filter command.

Just FYI, other commands that take row conditions:

  • take until
  • take while
  • skip until
  • skip while
  • any
  • all

We can either move these to the parser as well or make them accept a closure instead of row condition.

User-Facing Changes

New filter command which replaces where -b functionality.

Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo test --workspace to check that all tests pass

After Submitting

If your PR had any user-facing changes, update the documentation after the PR is merged, if necessary. This will help us keep the docs up to date.

@kubouch kubouch changed the title Start removing row condition syntax shape Move 'where' to parser keywords; Add 'filter' command Dec 10, 2022
@kubouch
kubouch marked this pull request as ready for review December 10, 2022 14:06
@fdncred

fdncred commented Dec 10, 2022

Copy link
Copy Markdown
Contributor

I personally think row conditions are a bit too magical except for where because it's expected. So, my vote is to remove the row condition from the commands you listed and make them closures. I like the filter command as well. It may take a bit of getting used to for where -b users but a deprecation message should help with that.

@kubouch
kubouch merged commit 6b4282e into nushell:main Dec 10, 2022
kubouch added a commit that referenced this pull request Dec 10, 2022
# Description

This PR changes some commands that previously accepted row conditions
(like `$it > 5`) as parameter to accept closures instead. The reasons
are:
a) The commands would need to move into parser keywords in the future
while they feel more like commands to be implemented in Nushell code as
a part of standard library.
b) In scripts, it is useful to store the predicate condition in a
variable which you can't do with row conditions.
c) These commands are not used that often to benefit enough from the
shorter row condition syntax

# User-Facing Changes

The following commands now accept **closure** instead of a **row
condition**:
- `take until`
- `take while`
- `skip until`
- `skip while`
- `any`
- `all`

This is a part of an effort to move away from shape-directed parsing.
Related PR: #7365

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
@sholderbach sholderbach added the notes:breaking-changes Noted in Breaking Changes section label Dec 10, 2022
sholderbach added a commit to sholderbach/nushell that referenced this pull request Jan 4, 2023
- Remove commented out example that is unnecessary after nushell#7365
- remove unnecessary closure map
sholderbach added a commit that referenced this pull request Jan 4, 2023
- Remove commented out example that is unnecessary after #7365
- remove unnecessary closure map
Bahex added a commit that referenced this pull request May 13, 2025
# Description

- `where` used to be able to filter using stored closures at some point
using a flag.
  > #5955
- This was later removed and `filter` command added to cover the use
case.
  > #7365

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

```nushell
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.
```nushell
[[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

---------

Co-authored-by: Bahex <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:breaking-changes Noted in Breaking Changes section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants