Design for Something Like FLIP To Reverse Arguments

One Solution: REFRAMER And INTEGER Indexing

The REFRAMER is something that lets you get a chance to hook a function call after it has accumulated its arguments, but before it has done typechecking:

Introducing REFRAMER: Close Cousin to ENCLOSE

Then, FRAME! now supports INTEGER! indexing... as a way of accessing non-refinement parameters as they would be seen on the interface:

Picking Function Arguments By Integer

So just make a REFRAMER that adds no arguments, and does a parameter switcheroo before executing the frame:

flip: reframer lambda [f [frame!]] [
    let temp: f.2
    f.2: f.1
    f.1: temp
    eval f
]

>> flip append [a b c] [d e]
== [d e [a b c]]

>> flip divide 10 20
== 2

The Definition Could Be Briefer...

Right now, the SWAP operation doesn't act on variables... it acts on series, and errors on WORD!s. I kind of feel like swapping variables is the more common intent, not entirely sure what the ramifications of overloading the SWAP operation to do something that different are.

But it could be this short, for those who seek brevity:

flip: reframer f -> [swap $f.1 $f.2, eval f]

cc: @bradrn

1 Like