Avoiding Having To Name Higher Order Function Variants

Rebol's use of BLOCK! as currency for code distinguishes it from other languages, where the currency of code is pretty much always a function.

Ren-C actually began to allow for the use of functions as branches, which even received the value that triggered the branch (if it wished):

>> if 1 + 2 (does [print "Branch running, no arguments."])
Branch running, no arguments.

>> if 1 + 2 (func [x] [print ["Branch received:" x]])
Branch received: 3

But what about concepts like FOR-EACH? They take a quoted argument that serves as the variable to bind to a block that serves as the loop body:

>> for-each x [1 2 3] [print [x]]
1
2
3

But when you're passing a function for the code to run, you don't need to name a variable. Today, you leave it blank:

>> for-each _ [1 2 3] (func [x] [print [x]])
1
2
3  ; this doesn't actually work today, but it should, so pretend it does

We can also write this in lambda form:

>> for-each _ [1 2 3] (x => print [x])
1
2
3  ; this doesn't actually work today, but it should, so pretend it does

Better, but the blank still feels a bit ugly. Can we do better?

Concept: Use <skip>pable quoted arguments

>> for-each [1 2 3] (y => print [y])
1
2
3

>> for-each 'x [1 2 3] [print x]
1
2
3

The cost here is that you have to signal somehow that you don't want a plain evaluation of your data. e.g.:

>> x: [1 2 3]
>> for-each x (y => print [y])
1
2
3

At the cost of forcing you to put a tick mark on variables when you want to use a block for code, it paves a way to the clean use of functions...the way that people are used to from other languages. And as lambdas get more powerful and able to do implicit specializations, I think the desirability of using this form will go way up.

What do people think? Is having to put the tick mark on the variables a bad thing? It feels kind of good to me actually--because it hints at the callsite that you will not be evaluating that argument.

2 Likes

There are a few places where I am always unsure wether I have to use the tick mark or not.
Used this way, there is a clear rule, so I like it.

2 Likes

It's interesting that Ren-C makes it possible to even quote leftward, hence we get the likes of:

 switch type of value [...]

That seems it would read uglier as:

 switch 'type of value [...]

But is that a case where it would be a better hint at what's going on? I'd be a bit disappointed because it actually takes quite a lot of work to make it possible to not have that tick there.

Maybe it would be a better hint, but still disturbing.
For me without the tick it's easier to understand what is meant, though my mental picture sees type as the active part. To understand what's really going on I need a little concentration.