Issue with upvars

I’m experiencing an issue regarding upvars. Its application is an update to the Streams library, but I’ll keep it as simple as possible for now. I do hope that someone will find a solution to the version I’m presenting here.

One use of upvars is as a parameter to be used in an embedded script. A well-known example is with the for block, e.g.:
untitled script pic 240

Another example is in the “Iteration, composition” library:
untitled script pic 241

In both cases the upvar (i , or #) is maintained by the HOF-block (for, or cascade ) and used by the embedded command (change) or function (+), respectively. And it works very well.

But if the evaluation of the script is somehow postponed, an issue may arise. I made a block new applicator:

… that will create an anonymous HIGHER ORDER FUNCTION (HOF), applying an embedded function on an index; the latter corresponds with the number of times the HOF was called.

So, for example:

Stand-alone, this works well. But if I create another HOF with the same block, something weird happens:

I can change the name of any upvar, so I can repair it:

… but for a library function, I don’t want the user to have to remember changing upvar names from the default.

So this is my request: can anyone think of a way to prevent this mix-up of upvars?

This links to the project.

does index need to be an upvar here? you already call the function with it. why not just create a local variable in the block instead?

Do you mean, something like this: … ?

It works, but it’s also a lot of hassle.

that's not what i meant, and i have no clue how that works. when i say you call the function with the variable, i mean you gave it as an input. use the input.


Yeah, that works, too. Still I’d prefer to use upvars for referring to any inputs to the (callback function that are supplied by the block - it’s more intuitive for the user, I think. The approach you’re advocating is (IMO) second-best, so I’ll be using that for now - i.e. until anyone invents a way around the upvars issue, or a more local variety of upvars is introduced. Thanks for thinking with me!