New version of loop


#1

In 8th I see this construct:

Function n times

Which is the same as Rebol’s

Loop n [ ... ]

But doesn’t this look less busy?

Times n :function

Loop could be changed to look to see if its parameter is a function to do this automatically, but currently it’s a native and not in user space.


#2

Modulo a small bug I just fixed (and test added), LOOP does do this:

>> foo: does [print "Hi"]
== make action! [[] [...]]

>> loop 3 :foo
Hi
Hi
Hi

Also, (the recent change to the loop protocol made more looping constructs accept action!s). It has been intended that any “branch” slot which would accept a block would also accept an action!, just a matter of doing it.


#3

That’s kinda cool. I guess I missed the announce.


#4

The more interesting feature of branches as functions is when they get an argument. ALSO, which runs whenever its left hand side is not null, will pass any arity-1 function what it got:

>> case [
       1 > 2 [10]
       1 < 2 [20]
   ] also :print
20

You can create functions on the spot with lambda (infix form, =>)

>> case [
       1 > 2 [10]
       1 < 2 [20]
   ] also val => [
       print ["A branch in the case ran and gave" val]
   ]
A branch in the case ran and gave 20

This is supposed to be systemic. So IF itself works this way.

>> data: [a 10 b 20]
>> if try select data 'b 
       val => [print ["If selected a value and it was" val]]
If selected a value and it was 20

Note that infix lambda is variadic, and knows a few tricks:

>> plus-one: (x => x + 1)
>> plus-one 10
== 11

>> no-arg: (=> print "woo")
>> no-arg
woo

I think the leaning for lambdas is they…like DOES…will not have a RETURN/LEAVE of their own. So if you say RETURN inside the body of a lambda, you use whatever RETURN was already in effect.

I had a weird idea that you would do multi-arg lambdas with paths:

>> sum2: (a/b => a + b)
>> sum2 10 20
== 30

Saves a little typing…and maybe a little visually tighter than ([a b] => a + b). Cool, or too weird?