Skip to content

Commit 2fbd825

Browse files
gaetboutLeonard Paturel
andauthored
2.6.0: use while let (#284)
## Pull Request type Please check the type of change your PR introduces: - [ ] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [x] Refactoring (no functional changes, no API changes) - [ ] Build-related changes - [ ] Documentation content changes - [ ] Other (please describe): Issue Number: N/A ## What is the new behavior? Update the code to make use of `while let Option::Some(x)` where possible --------- Co-authored-by: Leonard Paturel <leonardpaturel@gmail.com>
1 parent 6c2e0bb commit 2fbd825

12 files changed

Lines changed: 142 additions & 205 deletions

File tree

src/bytes/src/utils.cairo

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@ use keccak::{u128_to_u64, u128_split as u128_split_to_u64, cairo_keccak};
88
fn keccak_u128s_be(mut input: Span<u128>, n_bytes: usize) -> u256 {
99
let mut keccak_input = array![];
1010
let mut size = n_bytes;
11-
loop {
12-
match input.pop_front() {
13-
Option::Some(v) => {
14-
let value_size = uint_min(size, 16);
15-
keccak_add_uint128_be(ref keccak_input, *v, value_size);
16-
size -= value_size;
17-
},
18-
Option::None => { break; },
11+
while let Option::Some(v) = input
12+
.pop_front() {
13+
let value_size = uint_min(size, 16);
14+
keccak_add_uint128_be(ref keccak_input, *v, value_size);
15+
size -= value_size;
1916
};
20-
};
2117

2218
let aligned = n_bytes % 8 == 0;
2319
if aligned {

src/data_structures/src/array_ext.cairo

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,8 @@ impl ArrayImpl<T, +Copy<T>, +Drop<T>> of ArrayTraitExt<T> {
7878
}
7979

8080
fn concat_span<+Destruct<T>>(ref self: Array<T>, mut arr2: Span<T>) {
81-
loop {
82-
match arr2.pop_front() {
83-
Option::Some(elem) => self.append(*elem),
84-
Option::None => { break; }
85-
};
81+
while let Option::Some(elem) = arr2.pop_front() {
82+
self.append(*elem);
8683
}
8784
}
8885

@@ -195,29 +192,25 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
195192

196193
fn occurrences_of<+PartialEq<T>>(mut self: Span<T>, item: T) -> usize {
197194
let mut count = 0_usize;
198-
loop {
199-
match self.pop_front() {
200-
Option::Some(v) => { if *v == item {
201-
count += 1;
202-
} },
203-
Option::None => { break count; },
204-
};
205-
}
195+
while let Option::Some(v) = self.pop_front() {
196+
if *v == item {
197+
count += 1;
198+
}
199+
};
200+
count
206201
}
207202

208203
fn min<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<T> {
209204
let mut min = match self.pop_front() {
210205
Option::Some(item) => *item,
211206
Option::None => { return Option::None; },
212207
};
213-
loop {
214-
match self.pop_front() {
215-
Option::Some(item) => { if *item < min {
216-
min = *item
217-
} },
218-
Option::None => { break Option::Some(min); },
219-
};
220-
}
208+
while let Option::Some(item) = self.pop_front() {
209+
if *item < min {
210+
min = *item
211+
}
212+
};
213+
Option::Some(min)
221214
}
222215

223216
fn index_of_min<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<usize> {
@@ -227,31 +220,28 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
227220
Option::Some(item) => *item,
228221
Option::None => { return Option::None; },
229222
};
230-
loop {
231-
match self.pop_front() {
232-
Option::Some(item) => { if *item < min {
223+
while let Option::Some(item) = self
224+
.pop_front() {
225+
if *item < min {
233226
index_of_min = index + 1;
234227
min = *item;
235-
} },
236-
Option::None => { break Option::Some(index_of_min); },
228+
}
229+
index += 1;
237230
};
238-
index += 1;
239-
}
231+
Option::Some(index_of_min)
240232
}
241233

242234
fn max<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<T> {
243235
let mut max = match self.pop_front() {
244236
Option::Some(item) => *item,
245237
Option::None => { return Option::None; },
246238
};
247-
loop {
248-
match self.pop_front() {
249-
Option::Some(item) => { if *item > max {
250-
max = *item
251-
} },
252-
Option::None => { break Option::Some(max); },
253-
};
254-
}
239+
while let Option::Some(item) = self.pop_front() {
240+
if *item > max {
241+
max = *item
242+
}
243+
};
244+
Option::Some(max)
255245
}
256246

257247
fn index_of_max<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<usize> {
@@ -261,16 +251,15 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
261251
Option::Some(item) => *item,
262252
Option::None => { return Option::None; },
263253
};
264-
loop {
265-
match self.pop_front() {
266-
Option::Some(item) => { if *item > max {
254+
while let Option::Some(item) = self
255+
.pop_front() {
256+
if *item > max {
267257
index_of_max = index + 1;
268258
max = *item
269-
} },
270-
Option::None => { break Option::Some(index_of_max); },
259+
}
260+
index += 1;
271261
};
272-
index += 1;
273-
}
262+
Option::Some(index_of_max)
274263
}
275264

276265
fn dedup<+PartialEq<T>>(mut self: Span<T>) -> Array<T> {
@@ -281,28 +270,23 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
281270
let mut last_value = self.pop_front().unwrap();
282271
let mut ret = array![*last_value];
283272

284-
loop {
285-
match self.pop_front() {
286-
Option::Some(v) => { if (last_value != v) {
273+
while let Option::Some(v) = self
274+
.pop_front() {
275+
if (last_value != v) {
287276
last_value = v;
288277
ret.append(*v);
289-
}; },
290-
Option::None => { break; }
278+
}
291279
};
292-
};
293280

294281
ret
295282
}
296283

297284
fn unique<+PartialEq<T>>(mut self: Span<T>) -> Array<T> {
298285
let mut ret = array![];
299-
loop {
300-
match self.pop_front() {
301-
Option::Some(v) => { if !ret.contains(*v) {
302-
ret.append(*v);
303-
} },
304-
Option::None => { break; }
305-
};
286+
while let Option::Some(v) = self.pop_front() {
287+
if !ret.contains(*v) {
288+
ret.append(*v);
289+
}
306290
};
307291
ret
308292
}

src/linalg/src/kron.cairo

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,15 @@ fn kron<T, +Mul<T>, +AddEq<T>, +Zeroable<T>, +Copy<T>, +Drop<T>,>(
2222

2323
// [Compute] Kronecker product in a loop
2424
let mut array = array![];
25-
loop {
26-
match xs.pop_front() {
27-
Option::Some(x_value) => {
28-
let mut ys_clone = ys;
29-
loop {
30-
match ys_clone.pop_front() {
31-
Option::Some(y_value) => { array.append(*x_value * *y_value); },
32-
Option::None => { break; },
33-
};
34-
};
35-
},
36-
Option::None => { break; },
25+
26+
while let Option::Some(x_value) = xs
27+
.pop_front() {
28+
let mut ys_clone = ys;
29+
while let Option::Some(y_value) = ys_clone
30+
.pop_front() {
31+
array.append(*x_value * *y_value);
32+
}
3733
};
38-
};
3934

4035
Result::Ok(array)
4136
}

src/linalg/src/norm.cairo

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@ fn norm<T, +Into<T, u128>, +Zeroable<T>, +Copy<T>>(
1313
mut xs: Span<T>, ord: u128, iter: usize
1414
) -> u128 {
1515
let mut norm: u128 = 0;
16-
loop {
17-
match xs.pop_front() {
18-
Option::Some(x_value) => {
19-
if ord == 0 {
20-
if (*x_value).is_non_zero() {
21-
norm += 1;
22-
}
23-
} else {
24-
norm += pow((*x_value).into(), ord);
16+
while let Option::Some(x_value) = xs
17+
.pop_front() {
18+
if ord == 0 {
19+
if (*x_value).is_non_zero() {
20+
norm += 1;
2521
}
26-
},
27-
Option::None => { break; },
22+
} else {
23+
norm += pow((*x_value).into(), ord);
24+
};
2825
};
29-
};
3026

3127
if ord == 0 {
3228
return norm;

src/math/src/gcd_of_n_numbers.cairo

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ fn gcd(mut n: Span<u128>) -> u128 {
1111
panic_with_felt252('EI')
1212
}
1313
let mut a = *n.pop_front().unwrap();
14-
loop {
15-
match n.pop_front() {
16-
Option::Some(b) => { a = gcd_two_numbers(a, *b); },
17-
Option::None => { break a; },
18-
};
19-
}
14+
while let Option::Some(b) = n.pop_front() {
15+
a = gcd_two_numbers(a, *b);
16+
};
17+
a
2018
}
2119

2220
// Internal function to calculate the gcd between two numbers

src/math/src/lcm_of_n_numbers.cairo

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@ fn lcm<T, +Into<T, u128>, +Into<u128, T>, +Mul<T>, +Div<T>, +Copy<T>, +Drop<T>>(
1919
return Result::Err(LCMError::EmptyInput);
2020
}
2121
let mut a = *n.pop_front().unwrap();
22-
loop {
23-
match n.pop_front() {
24-
Option::Some(b) => {
25-
let gcd: T = gcd_two_numbers(a.into(), (*b).into()).into();
26-
a = (a * *b) / gcd;
27-
},
28-
Option::None => { break Result::Ok(a); },
22+
while let Option::Some(b) = n
23+
.pop_front() {
24+
let gcd: T = gcd_two_numbers(a.into(), (*b).into()).into();
25+
a = (a * *b) / gcd;
2926
};
30-
}
27+
Result::Ok(a)
3128
}

src/math/src/sha256.cairo

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,17 @@ fn sha256(mut data: Array<u8>) -> Array<u8> {
8282

8383
fn from_u32Array_to_u8Array(mut data: Span<u32>) -> Array<u8> {
8484
let mut result = array![];
85-
loop {
86-
match data.pop_front() {
87-
Option::Some(val) => {
88-
let mut res = (*val & 0xff000000) / 0x1000000;
89-
result.append(res.try_into().unwrap());
90-
res = (*val & 0xff0000) / 0x10000;
91-
result.append(res.try_into().unwrap());
92-
res = (*val & 0xff00) / 0x100;
93-
result.append(res.try_into().unwrap());
94-
res = *val & 0xff;
95-
result.append(res.try_into().unwrap());
96-
},
97-
Option::None => { break; },
85+
while let Option::Some(val) = data
86+
.pop_front() {
87+
let mut res = (*val & 0xff000000) / 0x1000000;
88+
result.append(res.try_into().unwrap());
89+
res = (*val & 0xff0000) / 0x10000;
90+
result.append(res.try_into().unwrap());
91+
res = (*val & 0xff00) / 0x100;
92+
result.append(res.try_into().unwrap());
93+
res = *val & 0xff;
94+
result.append(res.try_into().unwrap());
9895
};
99-
};
10096
result
10197
}
10298

@@ -167,21 +163,17 @@ fn create_message_schedule(data: Span<u32>, i: usize) -> Span<u32> {
167163

168164
fn from_u8Array_to_u32Array(mut data: Span<u8>) -> Array<u32> {
169165
let mut result = array![];
170-
loop {
171-
match data.pop_front() {
172-
Option::Some(val1) => {
173-
let val2 = data.pop_front().unwrap();
174-
let val3 = data.pop_front().unwrap();
175-
let val4 = data.pop_front().unwrap();
176-
let mut value = (*val1).into() * 0x1000000;
177-
value = value + (*val2).into() * 0x10000;
178-
value = value + (*val3).into() * 0x100;
179-
value = value + (*val4).into();
180-
result.append(value);
181-
},
182-
Option::None => { break; },
166+
while let Option::Some(val1) = data
167+
.pop_front() {
168+
let val2 = data.pop_front().unwrap();
169+
let val3 = data.pop_front().unwrap();
170+
let val4 = data.pop_front().unwrap();
171+
let mut value = (*val1).into() * 0x1000000;
172+
value = value + (*val2).into() * 0x10000;
173+
value = value + (*val3).into() * 0x100;
174+
value = value + (*val4).into();
175+
result.append(value);
183176
};
184-
};
185177
result
186178
}
187179

0 commit comments

Comments
 (0)