haumea-lang / haumea-rs

Haumea is an experimental language designed to be simple and easy to learn and program in.
MIT License
7 stars 2 forks source link

Add "n through n" (for-each loop) #8

Closed bates64 closed 7 years ago

bates64 commented 7 years ago

Looking at the tutorial, this:

for each y in 1 to 5 do
  display(y)
end

Doesn't display 5. (1 2 3 4)

I propose another expression:

for each y in 1 through 5 do
  display(y)
end

Which displays 1 2 3 4 5. Alternatively, 1 up to 5?

Through definition regarding this:

During the whole of a period of time until the end of it.

BookOwl commented 7 years ago

I made it not include the upper bound because when you work with arrays, the length of an array is always one more than the index of the last element, although if you can iterate directly over the items of an array using for each, it might not matter.

I wouldn't want to change the range syntax to 1 up to 5 because you can include a by clause to make it 5 to 0 by -1, which wouldn't make sense with 5 up to 0 by -1.

Speaking of which, I just realized that the code generated for for each loops that iterate backwards is broken! D:

bates64 commented 7 years ago

Sure! Having the through syntax there couldn't hurt, though, could it? :stuck_out_tongue_winking_eye:

BookOwl commented 7 years ago

I'll think about it, but first I need to make it work! :P

BookOwl commented 7 years ago

For future reference, @nanalan did this.