Collector In Disguise

So, this is somewhat trivial. But what if you wanted a function that instead of RETURN ending the function, it just added to the results and moved on? We might call this a COLLECTOR.

Let's imagine that we like KEEP better than renaming to RETURN

collector: func [spec block] [return func spec compose [collect (block)]]
; ^-- Ren-C semantics for COMPOSE, BLOCK! only splices if ((...))

c: collector [x] [
    keep x + 1
    keep x + 2
    keep x + 3
]

>> c 10
== [11 12 13]

>> c 20
== [21 22 23]

>> c 30
== [31 32 33]
1 Like

For the right perspective here, this is something different than using of so called coroutines where the co-routine gets halted or paused to transfer control to another coroutine that can either finish or pass control over again.

How I solve this thing is just collecting all stuff in a block and return that.

I somehow can imagine some use of this in gaming where this would run and go calculate (perhaps in a loop) until some trigger (timer) goes off and then a break follows that results in the function to return all collected values.

Yes, I am actually in the midst of trying to suss out what it takes to implement coroutines. I just documented this because I had been offering it as a contrast, e.g. what coroutines aren't.

Coroutines require suspending evaluative state and resuming it...which is a very deep idea fraught with technical peril. But, I am making slow and steady progress on it. It's not something that happens overnight.