feat: added items command for Records#8640
Conversation
items command for Records
|
Was just playing around and had a panic. |
|
Good catch @fdncred, is there a reason why |
|
I have tried to implement > def items [f] { transpose | rename a b | each {|key| do $f $key.a $key.b } } and then: > {new: york, san: francisco} | items {|key value| echo $'($key)-($value)' }
╭───┬───────────────╮
│ 0 │ new-york │
│ 1 │ san-francisco │
╰───┴───────────────╯I have tried the example of @fdncred, and it gives an output which is probably not the expected one: ls | items {|k, v| print $'($k) ($v)' } 03/27/2023 05:37:13 pm
name #*message*-20221028-162839#
type file
size 130 B
modified Sat, 29 Oct 2022 10:40:36 +0200 (4 months ago)What is the expected result when |
|
I think it's important to understand what to do with tables since BTW - I get this now Another test The nushell table at the end doesn't look right to me. |
|
Just added support for For your example it actually looks good to me @fdncred You are doing an |
|
What does the python equivalent return? IIRC, this |
|
The Python equivalent looks like : for key, value in mydict.items():
print(key, value)Under the hood, it is a lazy generator returning a tuple containing (key, value) If we wanted to keep the same principle, it would be something like mydict | items | each { |keyval| echo $keyval.0 $keyval.1 }
I find it less concise and agreable to use but I'm open to feedback |
|
ok. I like the non-tuple version myself. Thanks for letting me know. |
|
While avoiding the intermediate record is also great for performance and gives the nicest I might caveat that having a command that just performs the iteration into a different structure would have benefits for other commands beyond Furthermore to be as performant as this implementation some additional heavy lifting would probably be necessary, which would be another trade-off. |
|
@sholderbach the user can still transform the record into the type it wants and pipe it into something like {a: 1, b: 2} | items { |key, value| {key: $key, value: $value } | reduce ...We could even add later {a: 1, b: 2} | items list
# returns [[a, 1], [b, 2]] |
|
Yeah, I would agree. Let's not make perfect the enemy of the good. I think having the ability to quickly write something to operate over records is more valuable right now than perfect iterator design (in a pythonesque or rustic way). |
|
Is it ready to merge then :) ? |
| PipelineData::ListStream(stream, ..) => { | ||
| let v: Vec<_> = stream.into_iter().collect(); | ||
| let cols = get_columns(&v); | ||
| let vals = get_values(&v, call.head, input_span)?; | ||
|
|
||
| Ok(cols | ||
| .into_iter() | ||
| .zip(vals.into_iter()) | ||
| .into_iter() | ||
| .map_while(run_for_each_item) | ||
| .into_pipeline_data(ctrlc)) | ||
| } |
There was a problem hiding this comment.
I think this would be inconsistent behavior. This accepts a table if the list is a PipelineData::ListStream but not if it is a ...::Value(Value::List{}) of records.
There was a problem hiding this comment.
Based on the type error messages I would say lets remove this match arm and only handle records and LazyRecords right now.
|
Instead of Otherwise, I don't mind having |
|
@Sygmei I think if you can resolve the points by @sholderbach and @kubouch we can land this. |
|
Yes ! I'll try to take some time today to finish this |
|
@fdncred should be good :) |
|
Thanks. Let's go! |
# Description This PR adds an `items` command which allows the user to iterate over both `columns` and `values` of a `Record<>` type at the same time.  # User-Facing Changes No breaking changes, only a new `items` command. # Formatting - `cargo fmt --all -- --check` 👌 - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` 👌 - `cargo test --workspace` 👌



Description
This PR adds an
itemscommand which allows the user to iterate over bothcolumnsandvaluesof aRecord<>type at the same time.User-Facing Changes
No breaking changes, only a new
itemscommand.Formatting
cargo fmt --all -- --check👌cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect👌cargo test --workspace👌