rhai/scripts/for3.rhai

31 lines
531 B
JavaScript
Raw Normal View History

2022-07-24 17:03:35 +02:00
//! This script runs for-loops with closures.
2022-01-20 05:06:36 +01:00
2021-12-31 08:59:13 +01:00
const MAX = 100;
const CHECK = ((MAX - 1) ** 2) * MAX;
print("Ready... Go!");
let now = timestamp();
print(`Creating ${MAX} closures...`);
let list = [];
2022-01-20 05:06:36 +01:00
// Loop over range
2021-12-31 08:59:13 +01:00
for i in 0..MAX {
list.push(|| i ** 2);
}
print(`Time = ${now.elapsed} seconds...`);
print(`Summing ${MAX} closures...`);
let sum = 0;
2022-01-20 05:06:36 +01:00
// Loop over array
2021-12-31 08:59:13 +01:00
for f in list {
sum += f.call();
}
print(`Sum = ${sum} (should be ${CHECK})`);
print(`Finished. Total run time = ${now.elapsed} seconds.`);