Mutable state is lousy for performance. Since the iteration protocol is used for things as basic as a for loop count up from 1 to n, it has to be very fast. If it used a mutable iterator object, getting that to be fast enough would be a nightmare. Using explicit external state makes it easy to translate a for loop into very fast, efficient code, equivalent to what you would write by hand with a while loop.
On Thu, Jun 12, 2014 at 11:59 AM, Philippe Lavoie < [email protected]> wrote: > Hi, > So I was seduced by a lot of things Julia had to offer and I found the > documentation really fun and helping. > > However, I do have some questions, one of them would be: "why iterator > functions pass state?". > In a more traditional language, you would address an iterator as such: > iter.next(), iter.done(), iter.popFront(), iter.front(), iter.empty(), > etc... > > I am curious to know why, in Julia, iterator functions pass around state: > init = start(iter) > (i, current) = next(iter, init) > done = (iter, current) > > First of all, I am not sure why a method called "start" would exist, given > the iterator might be provided by another function, like > "ascendingValues(myRedBlackTree)", which would return an iterator for > values in ascending order for example. > Second, given you have an iterator, why pass its state around instead of > just modifying it at every functional call, like traditionally (java, c++, > etc...). IMHO, the iterator interface should/could borrow from other > programming languages and could be something like: > done = isDone(iter) > next = next(iter) > > >
