First, a friendly message: most often you can solve things in a functional way, using map, fold, filter, etc. resort to such when possible, before reaching for "imperative style loops".
The Basics - The While Loop
The foundational structural imperative construct while likely needs no introduction. There is no do .. while, use explicit break condition in the loop instead.
x = 0
while x < 47
do-stuff-with x
x += 1
Func / Method and Soft Lambda Based Loops
All more complex iterators are implemented as funcs / methods taking a soft lambda (read #14 if you haven't already) as argument. The for-construct is syntactic sugar mapping to these methods.
list = [1, "foo", 3.14]
list.each |val|
say val
list.each-with-index |val, ix|
say "{val}, {ix}"
10.times |i|
say i
(1..10).each |i|
say i
(0...10).each |i|
say i
For Loops
For-loops in Onyx is just sugar for calls to de facto named methods each, each-index or each-with-index, depending on which values are used.
If the for-loop is kept in the language, it will likely be extended to handle more advanced iterations later on like SIMD-chunking etc.
[RFC] An abundance of notational styles are intially available - let's find the best fitting one and remove the others. Or - should it be kept at all? Simply stick to callables + soft lambdas and remove the for-construct completely?
list = [1, "foo", 3.14]
-- common variant, shown with two different nest starter tokens
for val in list => say val
for val, ix in list: say "{val}, {ix}"
for val in list
say val
for val, ix in list
say "{val}, {ix}"
for ,ix in list
say ix
for n in 1..10
say n
for n in 0...10
say n
-- more esoteric variants, will likely be ditched!
for ix:val in list
p "{val}, {ix}"
for ix: in list
say ix
for val[ix] in list
say "{val}, {ix}"
for [ix] in list
say ix
Iterators and Loops
First, a friendly message: most often you can solve things in a functional way, using map, fold, filter, etc. resort to such when possible, before reaching for "imperative style loops".
The Basics - The While Loop
The foundational structural imperative construct
while
likely needs no introduction. There is nodo .. while
, use explicit break condition in the loop instead.Func / Method and Soft Lambda Based Loops
All more complex iterators are implemented as funcs / methods taking a soft lambda (read #14 if you haven't already) as argument. The for-construct is syntactic sugar mapping to these methods.
For Loops
For-loops in Onyx is just sugar for calls to de facto named methods
each
,each-index
oreach-with-index
, depending on which values are used. If the for-loop is kept in the language, it will likely be extended to handle more advanced iterations later on like SIMD-chunking etc.[RFC] An abundance of notational styles are intially available - let's find the best fitting one and remove the others. Or - should it be kept at all? Simply stick to callables + soft lambdas and remove the for-construct completely?