For loop
Similar to Python, Menter does not know traditional indexed 'for' loops, only 'for each' loops.
Syntax:
With each iteration, the variable is assigned the next value from the iterable. Anything that is iterable can also be looped over by calling the forEach
method:
continue and break
The continue
and break
keywords work exactly as in other languages.
break
will break out of the current loop, continue
will skip the current iteration and continue with the next one, if there is one.
To learn more about how this works internally, read the chapter about value markers.
Iterators
Anything that implements an iterator()
method is iterable.
Iterators can be called manually and return themselves when called again.
It is recommended to have a local Menter server running whilst viewing some of the code boxes below, as the print output cannot be seen otherwise.
Let's go over some of the primitive types that are iterable:
List
Lists iterate over all contained elements:
If you need the index of the current element alongside the value, simply provide a parameter list with the size 2.
Objects
Since objects and lists are represented by the same data structure internally, their iterable works quite similar. When providing one variable as iterator parameter, only the values are returned. When providing two, the key is passed alongside the value.
String
Strings iterate over all their characters, in order of appearance.
Making your custom java type iterable
This chapter will assume you know how to create your own java type. To learn more about custom java types, see this section.
The only addition that has to be made to a type class is an iterator
method. As with all function signatures, the method has to return a Value
, which means that the iterator has to be wrapped inside a Value
object before returning it.
Or you can simply call the iterator method on another Value
instance.
A full class could look like this: