forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.nula
More file actions
105 lines (95 loc) · 2.64 KB
/
Copy pathlist.nula
File metadata and controls
105 lines (95 loc) · 2.64 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Nulang standard library — List operations over native arrays.
// Import: `import std.list`
//
// Arrays are built-in: [1, 2, 3] syntax, arr[i] indexing, arr.len().
// This module adds functional combinators.
/// Map a function over every element, returning a new array.
pub fn map[T, U](arr: [T], f: fn(T) -> U) -> [U] {
let result = [];
let mut i = 0;
let len = arr.len();
while i < len {
result.push(f(arr[i]));
i = i + 1;
};
result
}
/// Keep elements that satisfy a predicate.
pub fn filter[T](arr: [T], pred: fn(T) -> Bool) -> [T] {
let result = [];
let mut i = 0;
let len = arr.len();
while i < len {
let item = arr[i];
if pred(item) then { result.push(item); } else { };
i = i + 1;
};
result
}
/// Reduce from the left: fold([a,b,c], init, f) = f(f(f(init,a),b),c).
pub fn fold[T, U](arr: [T], init: U, f: fn(U, T) -> U) -> U {
let mut acc = init;
let mut i = 0;
let len = arr.len();
while i < len {
acc = f(acc, arr[i]);
i = i + 1;
};
acc
}
/// Append two arrays into a new array.
pub fn append[T](left: [T], right: [T]) -> [T] {
let result = [];
let mut i = 0;
while i < left.len() { result.push(left[i]); i = i + 1; };
let mut j = 0;
while j < right.len() { result.push(right[j]); j = j + 1; };
result
}
/// Reverse an array.
pub fn reverse[T](arr: [T]) -> [T] {
let result = [];
let mut i = arr.len();
while i > 0 { i = i - 1; result.push(arr[i]); };
result
}
/// Get the nth element (0-indexed), or return default if out of bounds.
pub fn nth[T](arr: [T], n: Int, default: T) -> T {
if n >= 0 && n < arr.len() then arr[n] else default
}
/// Take the first n elements.
pub fn take[T](arr: [T], n: Int) -> [T] {
let result = [];
let mut i = 0;
let limit = if n < arr.len() then n else arr.len();
while i < limit { result.push(arr[i]); i = i + 1; };
result
}
/// Drop the first n elements.
pub fn drop[T](arr: [T], n: Int) -> [T] {
let result = [];
let mut i = n;
let len = arr.len();
while i < len { result.push(arr[i]); i = i + 1; };
result
}
/// Check if any element satisfies a predicate.
pub fn any[T](arr: [T], pred: fn(T) -> Bool) -> Bool {
let mut i = 0;
let len = arr.len();
while i < len {
if pred(arr[i]) then { return true; } else { };
i = i + 1;
};
false
}
/// Check if all elements satisfy a predicate.
pub fn all[T](arr: [T], pred: fn(T) -> Bool) -> Bool {
let mut i = 0;
let len = arr.len();
while i < len {
if !pred(arr[i]) then { return false; } else { };
i = i + 1;
};
true
}