3 ms·
Wait, so you return `false` to terminate the iterator? Stupid!! Now I can’t have an iterator that returns Booleans. UPDATE: I misread the article. This isn’t w
by ashton314 3y ago
Wait, so you return `false` to terminate the iterator? Stupid!! Now I can’t have an iterator that returns Booleans.
UPDATE: I misread the article. This isn’t what happens. My bad.
Honestly, I feel like Go focuses so much on superficial simplicity that you wind up with bad/leaky abstractions all over the place as well as bad primitive obsession.
/rant
- marcopolo 3y agoSure you can. The bool is a signal telling the thing yielding values to stop.
- leijurv 3y agoNo that's not at all how this works. The iterator could "return" a boolean by passing a boolean to "yield". The return value of "yield" just indicates if the generator should continue, or stop because the actual loop has exited (due to break or return).
- ashton314 3y agoOh fie you’re right and I misread that. Thanks for correcting me. I’ve updated my comment.
- Jtsummers 3y ago> Now I can't have an iterator that returns Booleans. No, you've misunderstood it. The iterator function gets a yield function which can be called with a value (the value to be yielded). The yield function returns a boolean indicating whether the process should continue or terminate. func SomeIterator(yield func(bool) bool) bool { for whatever { if !yield(a_calculated_boolean_value) { return false } } return true } There, yielding boolean values without incident.