Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] reform, rejoin and separation characters Re:

From: rebol:techscribe at: 19-Sep-2000 10:23

Hi Terrence, you wrote:
>Been awhile since I have been able to chew the fat with my fellow rebols,
>please correct me if I am wrong, but it seems that rejoin is a reform where >the separation character is the empty string as opposed to the space.
There's more to it than that. What the two functions have in common is that they reduce their argument, thereby replacing words supplied to them in a block by the values these words are bound to. However, there are quite a few differences that are due to the fact that rejoin and reform descend from different functions that are intended to accomplish different tasks. In some instances rejoin and reform have similar results. (See the differences documented below.) The rejoin function descends from join, whereas the reform function descends from form. The form function (form is a native function) is used to convert any type of value to a string. The join function (join is a mezzanine function) appends its second argument to its first argument. The join function uses form, if its first argument is not a series! type value. (source form, source join). The behavior you observe (preserving spaces or not) results from the fact that the two functions derive (or extend) essentially different functions. Besides the preservation of spaces there are, however, a few more differences: 1. While reform accepts any datatype you must pass a block to rejoin:
>> reform 3
== "3"
>> rejoin 3
** Script Error: rejoin expected block argument of type: block. ** Where: rejoin 3
>> reform "abcdef"
== "abcdef"
>> rejoin "abcdef"
** Script Error: rejoin expected block argument of type: block. ** Where: rejoin "abcdef" This can become relevant when you are using rejoin or reform programmatically, and you don't know for a certainty, whether or not the argument you will supply to the resomething function will be a block or not. Another difference is the return value:
>> reform [%/d/ "directory"]
== "/d/ directory"
>> rejoin [%/d/ "directory"]
== %/d/directory
>> type? reform [%/d/ "directory"]
== string!
>> type? rejoin [%/d/ "directory"]
== file! While reform always returns a string, no matter what it's argument - recall that reform descendants from form, and form's purpose is to convert everything to a string - rejoin returns an argument whose datatype is the datatype of the first element in rejoin's block argument, provided that first element is a series value. After all, rejoin descends from join, and join's purpose is not to convert everything to a string, join's purpose is to join its second argument to its first argument, while preverving the datatype of its first argument, if that datatype is a series! type. If the first element in the block passed to rejoin is a string, or if it is not a series type argument, in these two cases only will rejoin return a value of type string! If you source reform and rejoin, you'll be able to see the difference:
>> source reform
reform: func [ "Forms a reduced block and returns a string." value "Value to reduce and form" ][ form reduce value ]
>> source rejoin
rejoin: func [ "Reduces and joins a block of values." block [block!] "Values to reduce and join" ][ block: reduce block append either series? first block [copy first block] [ form first block] next block ] In summary, use reform if you would have normally used form, but you must first reduce words contained in a block before the block is converted into a string:
>> a: 1
== 1
>> form [a]
== "a"
>> reform [a]
== "1" Use rejoin when you want to append multiple dereferenced words to the leading element, while preserving that leading element's datatype:
>> rejoin [%/d/ "directory"]
== %/d/directory
>That being the case, why not simply one function with different refinements? >Then one could separate with the character(s) of choice: > >rekindle/sepchar [ 1 2 3 ] " " == reform >rekindle/sepchar [ 1 2 3 ] "" == rejoin >rekindle/sepchar [ 1 2 3 ] ", " == my own home-cooked joinchar. outrageous! > >The RT people seem kinda resistant to this sort of streamlining for some >reason. We may have to rebel against the rebols to get this into REBOL. > >terrence-brannon: [[princepawn--yahoo--com] perl-refugee myth-gamer] >free-email: >free-usenet: >; all the above is real REBOL code, believe it or not. >
;- Elan [ : - ) ] author of REBOL: THE OFFICIAL GUIDE REBOL Press: The Official Source for REBOL Books visit me at