We can make a list with the List function. 0
import stdlib.collections.list.*;
exported func main() {
l = List<int>();
l.add(1);
l.add(3);
l.add(7);
foreach x in l {
println(x);
}
}
1
3
7
Another way to write the above:
import stdlib.collections.list.*;
exported func main() {
l = List<int>().add(1).add(3).add(7);
foreach x in l {
println(x);
}
}
1
3
7
As seen above, we can loop with the foreach statement.
We can loop over many things: lists, arrays, even integer ranges:
import stdlib.*;
import stdlib.math.*;
exported func main() {
foreach x in range(0, 3) {
println(x);
}
}
0
1
2
When iterating over collections, we can also get the index of the current iteration with the entries() function.
import stdlib.*;
import stdlib.collections.list.*;
exported func main() {
l = List<int>().add(1).add(3).add(7);
foreach [i, x] in l.entries() {
println(i + ": " + x);
}
}
0: 1
1: 3
2: 7
We can also do the above with a while loop:
import stdlib.*;
import stdlib.collections.list.*;
exported func main() {
l = List<int>().add(1).add(3).add(7);
i = 0;
while i < l.len() {
x = l.get(i);
println(i + ": " + x);
set i = i + 1;
}
}
0: 1
1: 3
2: 7
To interrupt a loop early and skip to the code after it, use the break keyword:
import stdlib.*;
import stdlib.collections.list.*;
exported func main() {
l = List<int>().add(1).add(3).add(7);
foreach [i, x] in l.entries() {
println(i + ": " + x);
if i == 1 {
break;
}
}
}
0: 1
1: 3
Vale's lists are like C#'s List, Java's ArrayList or C++'s vector; it's an array list, not a linked list.
One doesn't often need arrays because Lists are easier, but arrays can sometimes be faster.
There are two kinds of arrays in Vale:
If you know size is known at compile-time, then you can use a static-sized array.
[#5]int is a static-sized array of 5 integers.
There are three ways to make one:
The size is optional, [#][0, 2, 4, 6, 8] is also valid. Specify a "fill function": [#5](i => i * 2), see example.
To access the element at index 3 in an array arr, we use arr[3]. Note that the first element is index 0, so index 3 is the fourth element.
import stdlib.*;
exported func main() {
arr = [#5](i => i * 2);
foreach i in range(0, 5) {
println(arr[i]);
}
}
0
2
4
6
8
A runtime-sized array is like a List, in that its size can change. However, its capacity cannot change. 1
[]int is a runtime-sized array of integers.
To make a new runtime-sized array, we use the []type(capacity) syntax:
To access the element at index 3 in an array arr, we use arr[3]. Note that the first element is index 0, so index 3 is the fourth element.
We can also populate it with a function, with the []type(capacity, func) syntax.
The func will be called for each element.
In this example, x => x * 5 is the function that will populate the array.
import stdlib.*;
import stdlib.math.*;
import stdlib.stdin.*;
exported func main() {
n = stdinReadInt();
arr = [](n, x => x * 5);
foreach i in range(0, n) {
println(arr[i]);
}
}
3
0
5
10
This may seem unusual coming from other languages. This decision helps us not need default values for our elements.
stdinReadInt reads an integer from the user's keyboard. In this example, the user is entering 3.
stdinReadInt is temporary and experimental, expect the stdin/stdout API to change soon.
The type of arr here is []int.
Next: Functions