//
// Copyright 2024 Formata, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#[test([2, 3, 4, 5, 6])]
fn mutate(): vec {
let array = [1, 2, 3, 4, 5];
for (int i in array) {
// For-in loops have a few helpful "hidden" variables defined:
// "index" - the current index we are at in the array
// "first" - true if this element is the first element in the array
// "last" - true if this element is the last element in the array
// "length" - the length of the array (5 in this case)
array.set(index, i + 1);
}
return array;
}
#[test([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])]
fn standard_iteration(): vec {
let vals = [];
for (let i = 0; i < 10; i += 1) {
vals.push(i);
}
return vals;
}
#[test([1, 2, 3, 4, 5, 6])]
fn while_iteration(): vec {
let i = 1;
let vals = [];
while (i < 7) {
vals.push(i);
i += 1;
}
return vals;
}