Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table and list helper commands #967

Merged
merged 5 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions stdlib-candidate/std-rfc/conversions/into.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Convert a Nushell value to a list
#
# Primary useful for range-to-list,
# but other types are accepted as well.
#
# Example:
#
# 1..10 | into list
export def "into list" []: any -> list {
let input = $in
let type = ($input | describe --detailed | get type)
match $type {
range => {$input | each {||}}
list => $input
table => $input
_ => [ $input ]
}
}
23 changes: 0 additions & 23 deletions stdlib-candidate/std-rfc/get-column.nu

This file was deleted.

6 changes: 0 additions & 6 deletions stdlib-candidate/std-rfc/get-row.nu

This file was deleted.

8 changes: 8 additions & 0 deletions stdlib-candidate/std-rfc/tables/col-indices.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use ../conversions/into.nu *
use ./select-ranges.nu *

export def main [ ...ranges ] {
columns
| select ranges $ranges
| get item
}
6 changes: 6 additions & 0 deletions stdlib-candidate/std-rfc/tables/mod.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export use ./select-ranges.nu *
export use ./reject-ranges.nu *
export use ./select-column-ranges.nu *
export use ./reject-column-ranges.nu *
export use ./row-indices.nu *
export use ./col-indices.nu *
13 changes: 13 additions & 0 deletions stdlib-candidate/std-rfc/tables/reject-column-ranges.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use ./col-indices.nu *

# Relect a range of columns by their indices
#
# Example:
#
# ls | reject column-ranges 0 4 5 | first 3
export def "reject column-ranges" [
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe this is named wrong since 0 4 5 isn't a nushell range?

...ranges
] {
let column_selector = ($in | col-indices ...$ranges)
$in | reject ...$column_selector
}
14 changes: 14 additions & 0 deletions stdlib-candidate/std-rfc/tables/reject-ranges.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use ./row-indices.nu *

# Rejects one or more rows while keeping
# the original indices.
#
# Example - Rejects the first, fifth, and
# sixth rows from the table:
#
# ls / | reject ranges 0 4..5
export def "reject ranges" [ ...ranges ] {
enumerate
| flatten
| reject ...(row-indices ...$ranges)
}
25 changes: 25 additions & 0 deletions stdlib-candidate/std-rfc/tables/row-indices.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use ../conversions/into.nu *

# Return a list of indices
# for the provided ranges or indices.
# Primarily used as a helper for
# "select ranges" et. al.
#
# Example:
#
# row-indices 0 2..5 7..8
# # => ╭───┬───╮
# # => │ 0 │ 0 │
# # => │ 1 │ 2 │
# # => │ 2 │ 3 │
# # => │ 3 │ 4 │
# # => │ 4 │ 5 │
# # => │ 5 │ 7 │
# # => │ 6 │ 8 │
# # => ╰───┴───╯
export def main [ ...ranges ] {
$ranges
| reduce -f [] {|range,indices|
$indices ++ ($range | into list)
}
}
20 changes: 20 additions & 0 deletions stdlib-candidate/std-rfc/tables/select-column-ranges.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use ./col-indices.nu *

# Select a range of columns by their indices
#
# Example:
#
# ls | select column-ranges 0 4 5 | first 3
# # => ╭───┬────────────────────╮
# # => │ # │ name │
# # => ├───┼────────────────────┤
# # => │ 0 │ CITATION.cff │
# # => │ 1 │ CODE_OF_CONDUCT.md │
# # => │ 2 │ CONTRIBUTING.md │
# # => ╰───┴────────────────────╯
export def "select column-ranges" [
...ranges
] {
let column_selector = ($in | col-indices ...$ranges)
$in | select ...$column_selector
}
26 changes: 26 additions & 0 deletions stdlib-candidate/std-rfc/tables/select-ranges.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use ./row-indices.nu *

# Selects one or more rows while keeping
# the original indices.
#
# Example - Selects the first, fifth, and
# sixth rows from the table:
#
# ls / | select ranges 0 4..5
#
# Example - Select the 5th row:
#
# ls / | select 5
#
# Example - Select the 4th row.
# Note that the difference beteen this
# and `select 3` is that the index (#)
# column shows the *original* (pre-select)
# position in the table.
#
# ls | select ranges 3
export def "select ranges" [ ...ranges ] {
enumerate
| flatten
| select ...(row-indices ...$ranges)
}