-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_structure.nu
More file actions
72 lines (54 loc) · 1.61 KB
/
data_structure.nu
File metadata and controls
72 lines (54 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use util.nu *
## Queue (first in, first out [FIFO])
# TODO also have a [out, new_queue] form with a flag.
export def 'queue shift' [queue] {
{ out: ($queue | first),
new_queue: ($queue | skip 1) }
}
## Stack (last in, first out [LIFO])
### Pop
# TODO also have a [out, new_stack] form with a flag.
export def 'stack pop' [stack] {
{ out: ($stack | last),
new_stack: ($stack | drop) }
}
# Inserting a new element
export def 'set insert' [elem
maybe_set?
] {
let set = if (null? $maybe_set) { $in } else { $maybe_set }
if $elem not-in $set {
$set | append $elem
}
}
# Union
# Maybe use op.nu's `op`
export def 'set union' [set_l set_r] {
$set_l ++ $set_r | uniq
}
# Intersection
export def 'set intersection' [set_l set_r] {
$set_l | filter { |elem| $elem in $set_r }
}
# Difference
# $set - $set_b
export def 'set difference' [set_l set_r] {
$set_l | filter { |elem| $elem not-in $set_r }
}
# Symmetric Difference
export def 'set symmetric difference' [set_l set_r] {
$set_l ++ $set_r | 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.