-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathslice.nr
138 lines (118 loc) · 4.23 KB
/
slice.nr
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::append::Append;
impl<T> [T] {
/// Returns the length of the slice.
#[builtin(array_len)]
pub fn len(self) -> u32 {}
/// Push a new element to the end of the slice, returning a
/// new slice with a length one greater than the
/// original unmodified slice.
#[builtin(slice_push_back)]
pub fn push_back(self, elem: T) -> Self {}
/// Push a new element to the front of the slice, returning a
/// new slice with a length one greater than the
/// original unmodified slice.
#[builtin(slice_push_front)]
pub fn push_front(self, elem: T) -> Self {}
/// Remove the last element of the slice, returning the
/// popped slice and the element in a tuple
#[builtin(slice_pop_back)]
pub fn pop_back(self) -> (Self, T) {}
/// Remove the first element of the slice, returning the
/// element and the popped slice in a tuple
#[builtin(slice_pop_front)]
pub fn pop_front(self) -> (T, Self) {}
/// Insert an element at a specified index, shifting all elements
/// after it to the right
#[builtin(slice_insert)]
pub fn insert(self, index: u32, elem: T) -> Self {}
/// Remove an element at a specified index, shifting all elements
/// after it to the left, returning the altered slice and
/// the removed element
#[builtin(slice_remove)]
pub fn remove(self, index: u32) -> (Self, T) {}
/// Append each element of the `other` slice to the end of `self`.
/// This returns a new slice and leaves both input slices unchanged.
pub fn append(mut self, other: Self) -> Self {
for elem in other {
self = self.push_back(elem);
}
self
}
pub fn as_array<let N: u32>(self) -> [T; N] {
assert(self.len() == N);
let mut array = [crate::mem::zeroed(); N];
for i in 0..N {
array[i] = self[i];
}
array
}
// Apply a function to each element of the slice, returning a new slice
// containing the mapped elements.
pub fn map<U, Env>(self, f: fn[Env](T) -> U) -> [U] {
let mut ret = &[];
for elem in self {
ret = ret.push_back(f(elem));
}
ret
}
// Apply a function to each element of the slice and an accumulator value,
// returning the final accumulated value. This function is also sometimes
// called `foldl`, `fold_left`, `reduce`, or `inject`.
pub fn fold<U, Env>(self, mut accumulator: U, f: fn[Env](U, T) -> U) -> U {
for elem in self {
accumulator = f(accumulator, elem);
}
accumulator
}
// Apply a function to each element of the slice and an accumulator value,
// returning the final accumulated value. Unlike fold, reduce uses the first
// element of the given slice as its starting accumulator value.
pub fn reduce<Env>(self, f: fn[Env](T, T) -> T) -> T {
let mut accumulator = self[0];
for i in 1..self.len() {
accumulator = f(accumulator, self[i]);
}
accumulator
}
// Returns a new slice containing only elements for which the given predicate
// returns true.
pub fn filter<Env>(self, predicate: fn[Env](T) -> bool) -> Self {
let mut ret = &[];
for elem in self {
if predicate(elem) {
ret = ret.push_back(elem);
}
}
ret
}
// Flatten each element in the slice into one value, separated by `separator`.
pub fn join(self, separator: T) -> T
where
T: Append,
{
let mut ret = T::empty();
if self.len() != 0 {
ret = self[0];
for i in 1..self.len() {
ret = ret.append(separator).append(self[i]);
}
}
ret
}
// Returns true if all elements in the slice satisfy the predicate
pub fn all<Env>(self, predicate: fn[Env](T) -> bool) -> bool {
let mut ret = true;
for elem in self {
ret &= predicate(elem);
}
ret
}
// Returns true if any element in the slice satisfies the predicate
pub fn any<Env>(self, predicate: fn[Env](T) -> bool) -> bool {
let mut ret = false;
for elem in self {
ret |= predicate(elem);
}
ret
}
}