Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterators and Loops #12

Open
ozra opened this issue Feb 21, 2016 · 2 comments
Open

Iterators and Loops #12

ozra opened this issue Feb 21, 2016 · 2 comments

Comments

@ozra
Copy link
Owner

ozra commented Feb 21, 2016

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 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
@ozra ozra changed the title Iterators and Loops [RFC] Iterators and Loops Feb 21, 2016
@ozra ozra changed the title [RFC] Iterators and Loops Iterators and Loops Feb 21, 2016
@ozra ozra added the RFC label Feb 21, 2016
@stugol
Copy link
Contributor

stugol commented Feb 21, 2016

I say keep the common variant. But without the => syntax option.

@ozra
Copy link
Owner Author

ozra commented Feb 22, 2016

Noted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants