mirror of
https://github.com/nushell/nu_scripts.git
synced 2024-08-17 01:10:36 +03:00
Adding some examples of how to treat a list like other data structures. (#733)
A nu_101 script to give some examples of how to treat lists like other data structures that nushell doesn't have natively. Namely queues, stacks, and sets (and multisets sort of). I'm also considering making some examples for trees and graphs, probably using tables, although I might put them in another script so it doesn't get too long.
This commit is contained in:
parent
a690fcbaa9
commit
66aa80064e
98
sourced/nu_101/emulating_other_data_structures.nu
Normal file
98
sourced/nu_101/emulating_other_data_structures.nu
Normal file
@ -0,0 +1,98 @@
|
||||
# Some examples of how you can use nushell commands to treat lists like other data structures and perform equivalent data structure operations on them.
|
||||
|
||||
## Queue (first in, first out [FIFO])
|
||||
|
||||
let queue = [1 2 3]
|
||||
|
||||
let elem = 4
|
||||
|
||||
### Enqueue (push)
|
||||
|
||||
$queue | append $elem
|
||||
|
||||
### Dequeue (shift)
|
||||
|
||||
{ out: ($queue | first),
|
||||
new_queue: ($queue | skip 1) }
|
||||
|
||||
## Stack (last in, first out [LIFO])
|
||||
|
||||
### Push
|
||||
|
||||
$queue | append $elem
|
||||
|
||||
### Pop
|
||||
|
||||
{ out: ($queue | last),
|
||||
new_stack: ($queue | drop) }
|
||||
|
||||
## Set
|
||||
|
||||
# Ordered sets are similar to below, just taking more care of order when altering the sets since lists are already ordered.
|
||||
|
||||
let set = [1 2 3]
|
||||
|
||||
let elem = 4
|
||||
|
||||
let set_b = [2 3 4 5]
|
||||
|
||||
### Checking set membership
|
||||
|
||||
$elem in $set # false
|
||||
|
||||
2 in $set # true
|
||||
|
||||
### Inserting a new element
|
||||
|
||||
if $elem not-in $set { $set | append $elem }
|
||||
|
||||
# or
|
||||
|
||||
$set | append $elem | uniq
|
||||
|
||||
# Result: [1 2 3 4]
|
||||
|
||||
### Union
|
||||
|
||||
$set ++ $set_b | uniq
|
||||
|
||||
# Result: [1 2 3 4 5]
|
||||
|
||||
### Intersection
|
||||
|
||||
$set | filter { |elem| $elem in $set_b }
|
||||
|
||||
# Result: [2 3]
|
||||
|
||||
### Difference
|
||||
|
||||
# $set - $set_b
|
||||
|
||||
$set | filter { |elem| $elem not-in $set_b }
|
||||
|
||||
# or
|
||||
|
||||
# Result: [1]
|
||||
|
||||
### Symmetric Difference
|
||||
|
||||
$set ++ $set_b | uniq --unique
|
||||
|
||||
# Result: [1 4 5]
|
||||
|
||||
### Multiset (bag)
|
||||
|
||||
# Pretty much the same as a list but you can get the counts of the multiset elements with
|
||||
|
||||
[1 2 2 3] | uniq --count
|
||||
|
||||
# Result:
|
||||
# ╭───┬───────┬───────╮
|
||||
# │ # │ value │ count │
|
||||
# ├───┼───────┼───────┤
|
||||
# │ 0 │ 1 │ 1 │
|
||||
# │ 1 │ 2 │ 2 │
|
||||
# │ 2 │ 3 │ 1 │
|
||||
# ╰───┴───────┴───────╯
|
||||
|
||||
# The unique values along with how many times they are in the multiset/bag.
|
Loading…
Reference in New Issue
Block a user