Skip to content

Commit 5b95311

Browse files
authored
Additional std-rfc/conversions (#1032)
Adds additional conversions suggested by @Bahex in [on Discord](https://discord.com/channels/601130461678272522/1333517965722910802/1333792425658941441). Some renaming to match the "conversions" style as much as possible. Namely, using `into` in the command names: * `columns-into-table` * `record-into-columns` * `table-into-columns` I'm still not quite sure about: * `name-values` Most commands use either a verb or preposition rather than an adjective, so `named` didn't quite seem to work. It could be `list-into-record`, but `into record` itself already accepts a list. --- Also provided help, examples, and tests for all commands.
1 parent 1354506 commit 5b95311

File tree

11 files changed

+180
-24
lines changed

11 files changed

+180
-24
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Convert a list of columns into a table
2+
#
3+
# Examples:
4+
#
5+
# [
6+
# ([ 1 2 3 ] | wrap a)
7+
# ([ 4 5 6 ] | wrap b)
8+
# ([ 7 8 9 ] | wrap c)
9+
# ] | columns-into-table
10+
# => ╭───┬───┬───┬───╮
11+
# => │ # │ a │ b │ c │
12+
# => ├───┼───┼───┼───┤
13+
# => │ 0 │ 1 │ 4 │ 7 │
14+
# => │ 1 │ 2 │ 5 │ 8 │
15+
# => │ 2 │ 3 │ 6 │ 9 │
16+
# => ╰───┴───┴───┴───╯
17+
#
18+
# Can roundtrip with `table-into-columns`
19+
#
20+
# ls | table-into-columns | columns-into-table
21+
# => ╭───┬────────────────────────┬──────┬────────┬────────────────╮
22+
# => │ # │ name │ type │ size │ modified │
23+
# => ├───┼────────────────────────┼──────┼────────┼────────────────┤
24+
# => │ 0 │ into-list.nu │ file │ 378 B │ 40 minutes ago │
25+
# => │ 1 │ mod.nu │ file │ 28 B │ 41 minutes ago │
26+
# => │ 2 │ name-values.nu │ file │ 394 B │ 34 minutes ago │
27+
# => │ 3 │ record-into-columns.nu │ file │ 1.3 kB │ 27 minutes ago │
28+
# => ╰───┴────────────────────────┴──────┴────────┴────────────────╯
29+
export def main []: [list<table> -> table] {
30+
reduce {|it| merge $it}
31+
}

stdlib-candidate/std-rfc/conversions/into.nu renamed to stdlib-candidate/std-rfc/conversions/into-list.nu

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Convert a Nushell value to a list
22
#
3-
# Primary useful for range-to-list,
4-
# but other types are accepted as well.
3+
# Primary useful for range-to-list, but other types are accepted as well.
54
#
65
# Example:
76
#
@@ -15,4 +14,4 @@ export def "into list" []: any -> list {
1514
table => $input
1615
_ => [ $input ]
1716
}
18-
}
17+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export use ./into.nu *
1+
export use ./into-list.nu *
2+
export use ./columns-into-table.nu *
3+
export use ./name-values.nu *
4+
export use ./record-into-columns.nu *
5+
export use ./table-into-columns.nu *
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Assign keynames to a list of values, effectively converting the list to a record.
2+
#
3+
# Example:
4+
#
5+
# [ 1 2 3 ] | name-values a b c
6+
# => ╭───┬───╮
7+
# => │ a │ 1 │
8+
# => │ b │ 2 │
9+
# => │ c │ 3 │
10+
# => ╰───┴───╯
11+
export def main [...names: string]: [list -> record] {
12+
let IN = $in
13+
0.. | zip $IN | into record | rename ...$names
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Convert a record, where each value is a list, into a list of columns.
2+
# { a: [ 1 2 3 ], b: [ 4 5 6 ] } | record-into-columns
3+
# => ╭───┬───────────╮
4+
# => │ 0 │ ╭───┬───╮ │
5+
# => │ │ │ # │ a │ │
6+
# => │ │ ├───┼───┤ │
7+
# => │ │ │ 0 │ 1 │ │
8+
# => │ │ │ 1 │ 2 │ │
9+
# => │ │ │ 2 │ 3 │ │
10+
# => │ │ ╰───┴───╯ │
11+
# => │ 1 │ ╭───┬───╮ │
12+
# => │ │ │ # │ b │ │
13+
# => │ │ ├───┼───┤ │
14+
# => │ │ │ 0 │ 4 │ │
15+
# => │ │ │ 1 │ 5 │ │
16+
# => │ │ │ 2 │ 6 │ │
17+
# => │ │ ╰───┴───╯ │
18+
# => ╰───┴───────────╯
19+
# =>
20+
# This can be especially useful when combined with `columns-into-table`, as in:
21+
#
22+
# { a: [ 1 2 3 ], b: [ 4 5 6 ] } | record-into-columns
23+
# | columns-into-table
24+
# => ╭───┬───┬───╮
25+
# => │ # │ a │ b │
26+
# => ├───┼───┼───┤
27+
# => │ 0 │ 1 │ 4 │
28+
# => │ 1 │ 2 │ 5 │
29+
# => │ 2 │ 3 │ 6 │
30+
# => ╰───┴───┴───╯
31+
# =>
32+
export def main []: [record -> list] {
33+
items {|key, val| $val | wrap $key}
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Convert/split a table into a list of columns
2+
#
3+
# Examples:
4+
# ls | table-into-columns
5+
# => Returns a list of 4 tables, one for each of the `ls` columns
6+
#
7+
# Can be roundtripped with `columns-into-table`
8+
#
9+
# ls | table-into-columns | columns-into-table
10+
# => ╭───┬────────────────────────┬──────┬────────┬────────────────╮
11+
# => │ # │ name │ type │ size │ modified │
12+
# => ├───┼────────────────────────┼──────┼────────┼────────────────┤
13+
# => │ 0 │ into-list.nu │ file │ 378 B │ 40 minutes ago │
14+
# => │ 1 │ mod.nu │ file │ 28 B │ 41 minutes ago │
15+
# => │ 2 │ name-values.nu │ file │ 394 B │ 34 minutes ago │
16+
# => │ 3 │ record-into-columns.nu │ file │ 1.3 kB │ 27 minutes ago │
17+
# => ╰───┴────────────────────────┴──────┴────────┴────────────────╯
18+
export def main []: [table -> list<table>] {
19+
let IN = $in
20+
$IN | columns | each {|col| $IN | select $col}
21+
}

stdlib-candidate/std-rfc/tables/col-indices.nu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ../conversions/into.nu *
1+
use ../conversions/into-list.nu *
22
use ./select-ranges.nu *
33

44
export def main [ ...ranges ] {
@@ -12,4 +12,4 @@ export def main [ ...ranges ] {
1212
$in | columns
1313
| select ranges $indices
1414
| get item
15-
}
15+
}

stdlib-candidate/std-rfc/tables/reject-ranges.nu

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use ./row-indices.nu *
22

3-
# Rejects one or more rows while keeping
4-
# the original indices.
3+
# Rejects one or more rows while keeping the original indices.
54
#
6-
# Example - Rejects the first, fifth, and
7-
# sixth rows from the table:
5+
# Example - Rejects the first, fifth, and sixth rows from the table:
86
#
97
# ls / | reject ranges 0 4..5
108
export def "reject ranges" [ ...ranges ] {

stdlib-candidate/std-rfc/tables/row-indices.nu

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use ../conversions/into.nu *
1+
use ../conversions/into-list.nu *
22

3-
# Return a list of indices
4-
# for the provided ranges or indices.
5-
# Primarily used as a helper for
6-
# "select ranges" et. al.
3+
# Return a list of indices for the provided ranges or indices. Primarily used as a helper for "select ranges" et. al.
74
#
85
# Example:
96
#
@@ -22,4 +19,4 @@ export def main [ ...ranges ] {
2219
| reduce -f [] {|range,indices|
2320
$indices ++ ($range | into list)
2421
}
25-
}
22+
}

stdlib-candidate/std-rfc/tables/select-ranges.nu

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use ./row-indices.nu *
22

3-
# Selects one or more rows while keeping
4-
# the original indices.
3+
# Selects one or more rows while keeping the original indices.
54
#
6-
# Example - Selects the first, fifth, and
7-
# sixth rows from the table:
5+
# Example - Selects the first, fifth, and sixth rows from the table:
86
#
97
# ls / | select ranges 0 4..5
108
#
@@ -13,10 +11,8 @@ use ./row-indices.nu *
1311
# ls / | select 5
1412
#
1513
# Example - Select the 4th row.
16-
# Note that the difference between this
17-
# and `select 3` is that the index (#)
18-
# column shows the *original* (pre-select)
19-
# position in the table.
14+
#
15+
# Note that the difference between this and `select 3` is that the index (#) column shows the *original* (pre-select) position in the table.
2016
#
2117
# ls | select ranges 3
2218
export def "select ranges" [ ...ranges ] {

0 commit comments

Comments
 (0)