Redbol Adaptation to get /ONLY on APPEND, INSERT...

/ONLY is Dead...but Ren-C's flexibility means you can get it back if you want it, and not break a sweat doing so. You just become responsible for figuring out what its universal meaning is. (#goodluckwiththat)

Remember that AUGMENT lets you add parameters to functions.

>> foo: func [x] [print ["x is" mold x]]
>> parameters of :foo
== [x]

>> foo+: augment :foo [/only "An ONLY parameter"]
>> parameters of :foo+
== [x /only]

>> foo+ 10
x is 10

>> foo+/only 10
x is 10

But adding parameters doesn't actually do anything but expand the specification. You have to use another tool like ADAPT or ENCLOSE to make use of the new parameters that FOO never knew about.

>> foo++: adapt :foo+ [print either only ["/ONLY!"] ["no /ONLY"]]

>> foo++ 10
no /ONLY
x is 10

>> foo++/only 10
/ONLY!
x is 10

So here's an ONLIFY transformer that adds a refinement to a function, and then injects a little code to pre-process the parameter to make a splice (group isotope) via SPREAD if needed.

onlify: lambda [
    {Add /ONLY behavior to APPEND, INSERT, CHANGE}
    action [action?]
][
    adapt (augment :action [/only "Use quoted semantics for value"]) [
        all [not only, any-array? series, any-array? value] then [
            value: spread value
        ]
        ; ...fall through to normal handling
    ]
]

Then you can apply it to the functions:

append: my onlify  ; e.g. `append: onlify :append` 
insert: my onlify
change: my onlify

And there you have it:

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

>> append/only [a b c] [d e]
== [a b c [d e]]
2 Likes