File tree Expand file tree Collapse file tree 11 files changed +180
-24
lines changed
Expand file tree Collapse file tree 11 files changed +180
-24
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 *
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1- use ../conversions/into .nu *
1+ use ../conversions/into-list .nu *
22use ./select-ranges .nu *
33
44export def main [ ... ranges ] {
@@ -12,4 +12,4 @@ export def main [ ...ranges ] {
1212 $in | columns
1313 | select ranges $indices
1414 | get item
15- }
15+ }
Original file line number Diff line number Diff line change 11use ./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
108export def "reject ranges" [ ... ranges ] {
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11use ./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
2218export def "select ranges" [ ... ranges ] {
You can’t perform that action at this time.
0 commit comments