CHOP mezzanine or REMOVE refinements?
[1/2] from: pwawood:mango:my at: 15-Dec-2004 20:09
Gregg et al
I think that it would be most in keeping with the "Rebol way" if there were
two new refinements for REMOVE - REMOVE/BEFORE & REMOVE/AFTER. I think they
are relatively self-explanatory and they would be very readable:
remove/before mystring find mystring "Peter"
remove/after mystring find mystring "Wood"
Of course it would be great if the before and after refinements included an
implicit find so that you could code :
remove/before mystring "Peter"
remove/after mystring "Wood"
Is there anyway to add refinements to existing Rebol functions as mezzanine
code?
Regards
Peter
[2/2] from: carl::cybercraft::co::nz at: 19-Dec-2004 16:39
Hi Peter,
On Wednesday, 15-December-2004 at 20:09:44 you wrote,
>I think that it would be most in keeping with the "Rebol way" if there were
>two new refinements for REMOVE - REMOVE/BEFORE & REMOVE/AFTER. I think they
>are relatively self-explanatory and they would be very readable:
>
>remove/before mystring find mystring "Peter"
>remove/after mystring find mystring "Wood"
As FIND has a /TAIL refinement, you wouldn't gain much from that. ie...
>> mystring: "abcPeterdef"
== "abcPeterdef"
>> remove back find mystring "Peter"
== "Peterdef"
>> mystring
== "abPeterdef"
>>
>> mystring: "abcPeterdef"
== "abcPeterdef"
>> remove find/tail mystring "Peter"
== "ef"
>> mystring
== "abcPeteref"
>Of course it would be great if the before and after refinements included an
>implicit find so that you could code :
>
>remove/before mystring "Peter"
>remove/after mystring "Wood"
My feeling would be that adding a /last refinement to REMOVE would be useful, (and in
keeping with the /last in FIND.) remove/last would then remove the last character from
the string, with remove/last/part 4 removing the last four characters.
>Is there anyway to add refinements to existing Rebol functions as mezzanine
>code?
Yes - just re-write the function. A quick example (others I'm sure could come up with
something better)...
old-remove: :remove ; Keep original REMOVE since it's a native and so
; should be faster than a complete re-write in REBOL.
remove: func [
series
/last
/part range
][
either last [
either part [
old-remove/part skip tail series negate range range
][
old-remove back tail series
]
][
either part [old-remove/part series range][old-remove series]
]
series
]
>> remove "abcde"
== "bcde"
>> remove/part "abcde" 3
== "de"
>> remove/last "abcde"
== "abcd"
>> remove/last/part "abcde" 3
== "ab"
-- Carl Read.