Re: [Rd] An iteration protocol

2025-08-12 Thread Taras Zakharko
Great stuff, and I like the use of a sentinel as a terminator symbol. One aspect of this I would like to explore is that of a lazy sequence as a more fundamental language primitive. Generators in for loops are great, but generators returned by lapply() and friends would enable lazy functional t

Re: [Rd] An iteration protocol

2025-08-12 Thread Tomasz Kalinowski
Thank you Lionel, Peter, and Duncan! Some responses inline below: > Couldn't this all be done in a while or repeat loop? ... > Not as simple as yours, but I think a little clearer because it's more > concrete, less abstract. Indeed, that’s the trade-off! Explicit and verbose vs. simple, concise,

Re: [Rd] An iteration protocol

2025-08-12 Thread Lionel Henry via R-devel
Clever! If going for non-local returns, probably best for ergonomics to pass in a closure (see e.g. `callCC()`). If only to avoid accidental jumps while debugging. But... do we need more lazy evaluation tricks in the language or fewer? It's probably more idiomatic to express non-local returns with

Re: [Rd] An iteration protocol

2025-08-11 Thread Peter Meilstrup
Passing the sentinel value as an argument to the iteration method is the approach taken in my package `iterors` on CRAN. If the sentinel value argument is evaluated lazily, this lets you pass calls to things like 'stop', 'break' or 'return,' which will be called to signal end of iteration. This mak

Re: [Rd] An iteration protocol

2025-08-11 Thread Lionel Henry via R-devel
Hello, A couple of comments: - Regarding the closure + sentinel approach, also implemented in coro (https://github.com/r-lib/coro/blob/main/R/iterator.R), it's more robust for the sentinel to always be a temporary value. If you store the sentinel in a list or a namespace, it might inadverte

Re: [Rd] An iteration protocol

2025-08-11 Thread Duncan Murdoch
1. I'm not sure I see the need for the syntax change. Couldn't this all be done in a while or repeat loop? E.g. your example could keep the same definition of SampleSequence, then iterator <- SampleSequence(2) repeat { sample <- iterator() if (is.null(sample)) break print(sample)

[Rd] An iteration protocol

2025-08-11 Thread Tomasz Kalinowski
Hi all, A while back, Hadley and I explored what an iteration protocol for R might look like. We worked through motivations, design choices, and edge cases, which we documented here: https://github.com/t-kalinowski/r-iterator-ideas At the end of this process, I put together a patch to R (with tes