Skip to content

allow where to work with variables - #5955

Merged
fdncred merged 4 commits into
nushell:mainfrom
merelymyself:main
Jul 6, 2022
Merged

fdncred merged 4 commits into
nushell:mainfrom
merelymyself:main

Conversation

@merelymyself

Copy link
Copy Markdown
Contributor

Description

Fixes #5547. Adds an extra named, so the change is non-breaking.

Tests

Make sure you've done the following:

  • Add tests that cover your changes, either in the command examples, the crate/tests folder, or in the /tests folder.
  • Try to think about corner cases and various ways how your changes could break. Cover them with tests.
  • If adding tests is not possible, please document in the PR body a minimal example with steps on how to reproduce so one can verify your change works.

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 --features=extra -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo test --workspace --features=extra to check that all the tests pass

@sholderbach

Copy link
Copy Markdown
Member

The reedline release process causes cargo install to fall over so the failing virtualenv tests are no issue and can be rerun.
Would you mind adding an integration test for the new feature? I see the example is needed for your new flag. What are the points of breakage and potential confusion? If it is sensible we can accept breaking changes pre 1.0

@merelymyself

Copy link
Copy Markdown
Contributor Author

@sholderbach - I would prefer to implement a breaking change. The issue right now is that the different filters are not standardised, some allow for blocks (and hence use $in) while others ask for RowConditions (and hence use $it, and can just use ‘name’ instead of ‘$in.name’). We could standardise it to one or the other, but given the rest of nushell uses $in over $it, I would prefer all filters requiring Blocks instead of RowConditions. However, this will likely be a very large breaking change.

@sholderbach

sholderbach commented Jul 5, 2022 •

Copy link
Copy Markdown
Member

I agree, that it would be best to eliminate the $it/$in source of confusion. But the beauty of where's succinct Syntax should also be preserved, so i don't think we should for example require wrapping braces if not needed for obvious reasons.
But let us map out what potential ambiguities may arise from harmonizing the row expressions.

@fdncred

fdncred commented Jul 5, 2022

Copy link
Copy Markdown
Contributor

Looks good. Excited to play with this but will wait until after the release sometime today. Thanks!

@merelymyself

Copy link
Copy Markdown
Contributor Author

Maybe we can push this first to deal with the immediate issue, but I do think in the long-term it will be better to standardise the filters. That's probably way beyond my ability, though.

@fdncred

fdncred commented Jul 6, 2022

Copy link
Copy Markdown
Contributor

I agree that I'd like to be more standardized in our filtering. It seems that standardization is a bigger topic than can be resolved here. Let's discuss in discord more to figure out a way forward.

I've tested this PR and the old way still works with where like ls | where size > 5kb but it now also supports a new way that allows you to supply a previously composed filter like | where -b $a, which seems to unlock some filtering possibilities. Although, to a certain degree, this filter composition is a bit limited because you cannot supply cell paths (I don't think), which limits it's usefulness a bit.

Let's give it a try and see who we like this.

@fdncred
fdncred merged commit e77219a into nushell:main Jul 6, 2022
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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

where returns no results when passed a block stored in a variable (but each/reduce work as expected)

3 participants