We can make a list with the List function. 0
Another way to write the above:
As seen above, we can loop with the foreach statement.
We can loop over many things: lists, arrays, even integer ranges:
When iterating over collections, we can also get the index of the current iteration with the entries() function.
We can also do the above with a while loop:
To interrupt a loop early and skip to the code after it, use the break keyword:
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.
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.
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