Hacker News new | past | comments | ask | show | jobs | submit login

I really like that for loops can be expressions. It seems obvious in hindsight, but hindsight is always 20/20 :)



> It seems obvious in hindsight

It's not, because most languages don't have an `else` clause in their for loop (and in my experience with Python that clause is quite confusing so its use is not common).

And a for loop can be executed 0 times, so without a mechanism for a fallback it might not have a value to yield.


I think a language where it is an expression should also let the loop accumulate a value. In those cases you could just have it return the identity/base type of whatever you are accumulating.

Say like the for loops in racket, or my own loops for guile scheme: https://git.sr.ht/%7Ebjoli/goof-loop


> And a for loop can be executed 0 times, so without a mechanism for a fallback it might not have a value to yield.

I would think that and the similar case where no iteration hits break are solvable by having a for loop return an optional type.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: