3 ms·
> With a higher level mechanism, we can separate the behavior that we want for loops from the behavior we want for everything else in a nice, clean way. Yes! O
by pcwalton 11y ago
> With a higher level mechanism, we can separate the behavior that we want for loops from the behavior we want for everything else in a nice, clean way.
Yes! One decisive advantage that high-level iteration constructs have over for loops is that they're easier to optimize.
One obvious example here is bounds checks: there's no need to have infrastructure to optimize out bounds checks over arrays during loops if you have a construct that eliminates the indexing entirely.
- btrask 11y agoGenuine question: what do you do when you want to implement your own loop mechanism, iterator, array type, or whatever?
- cokernel_hacker 11y agoIt is the job of the language to ensure that language primitives are reusable and composable. I think this would apply to whatever mechanism is designed to describe a loop's iteration space.
- Sean1708 11y agoMost languages use some sort of interface whereby if you implement certain methods for a type then that type can be iterated over using a for loop.