forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.nula
More file actions
65 lines (56 loc) · 1.59 KB
/
Copy pathset.nula
File metadata and controls
65 lines (56 loc) · 1.59 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
// Nulang standard library — Set operations.
// Language version: 2.0.0-alpha
// Import: `import stdlib::set`
//
// Sets are represented as arrays with no duplicates.
/// An empty set.
pub fn empty() { [] }
/// Check whether a value is in the set.
pub fn contains(set, value) {
for x in set {
if x == value then { return true } else {}
};
false
}
/// Add a value to the set (no-op if already present).
/// Returns a new set; the original is unchanged.
pub fn add(set, value) {
if contains(set, value) then set
else { perform Array.push(set, value) }
}
/// Remove a value from the set. Returns a new set.
pub fn remove(set, value) {
var result = [];
for x in set {
if x != value then { result = perform Array.push(result, x) } else {}
};
result
}
/// Number of elements in the set.
pub fn size(set) { perform Array.length(set) }
/// True when the set is empty.
pub fn is_empty(set) { perform Array.length(set) == 0 }
/// Union: all elements in a or b (no duplicates).
pub fn union(a, b) {
var result = a;
for x in b {
if contains(result, x) then {} else { result = perform Array.push(result, x) }
};
result
}
/// Intersection: elements present in both a and b.
pub fn intersect(a, b) {
var result = [];
for x in a {
if contains(b, x) then { result = perform Array.push(result, x) } else {}
};
result
}
/// Difference: elements in a that are not in b.
pub fn difference(a, b) {
var result = [];
for x in a {
if contains(b, x) then {} else { result = perform Array.push(result, x) }
};
result
}