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

Curious 'rejoin behavior

 [1/9] from: gjones05::mail::orion::org at: 30-May-2001 16:26


I was working through some xml to rebol conversions, and I noticed what seems to be curious behavior on the part of 'rejoin. I suspect that this is a bug, but maybe I have misunderstood the behavior of this word. example: price: 2 rejoin [ <bid> price </bid> ] ; returns: <bid2</bid>> ; shouldn't it be: <bid>2</bid> Bug or have I misunderstood some fundamental property of 'rejoin? --Scott Jones

 [2/9] from: joel:neely:fedex at: 30-May-2001 17:31


Hi, Scott, You understand it, you just don't know that you do! ;-) GS Jones wrote:
> I was working through some xml to rebol conversions, and I > noticed what seems to be curious behavior on the part of
<<quoted lines omitted: 7>>
> 'rejoin? >From this experiment we conclude that the result of REJOIN
has the same type as the first value in the block.
>> rejoin [[1 2 3] 4 5 [6 7 8]]
== [1 2 3 4 5 [6 7 8]]
>> rejoin ["1 2 3" 4 5 [6 7 8]]
== "1 2 3456 7 8" Several of the interesting data types in REBOL may be viewed as variations of string, since they can be handled as series of characters:
>> series? <tag>
== true
>> series? http://foo.com/
== true
>> any-string? <tag>
== true
>> any-string? http://foo.com/
== true
>> any-string? [joel--neely--fedex--com]
== true This can be convenient if we need to manipulate those values:
>> urlhost: http://foo.com/
== http://foo.com/
>> urlpath: "some/random/path"
== "some/random/path"
>> urlfile: %bletch.html
== %bletch.html
>> type? urlhost
== url!
>> type? urlpath
== string!
>> type? urlfile
== file!
>> rejoin [urlhost urlpath urlfile]
== http://foo.com/some/random/pathbletch.html
>> type? rejoin [urlhost urlpath urlfile]
== url! Since a tag is a "string-like" series, REJOIN operates on it in a "string-like" way, but retains the type of tag! for the result.
>> opentag: <bid>
== <bid>
>> type? opentag
== tag!
>> first opentag
== #"b"
>> second opentag
== #"i"
>> third opentag
== #"d" You were bitten by the negative consequence of this implicit stringization of the tag type. Here's an example of how it can be beneficial:
>> type? probe rejoin [
opentag { attr0="value0"} { attr1="value1"} ] <bid attr0="value0" attr1="value1"> == tag! One way to get your intended behavior (at least, I assume I know what you wanted ;-) is to consider the following:
>> price: 2
== 2
>> reduce [ <bid> price </bid> ]
== [<bid> 2 </bid>]
>> to-string reduce [ <bid> price </bid> ]
== "<bid>2</bid>" Hope this helps! -jn- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com

 [3/9] from: brian:hawley at: 30-May-2001 17:55


Scott Jones wrote:
>I was working through some xml to rebol conversions, and I noticed what >seems to be curious behavior on the part of 'rejoin. I suspect that
<<quoted lines omitted: 5>>
>Bug or have I misunderstood some fundamental property of 'rejoin? >--Scott Jones
Join and rejoin append the latter arguments to a copy of the first argument, and if the first argument is a string type (like tag!) the result will have the same type. You might want to change your code to this, for example: price: 2 rejoin ["" <bid> price </bid>] ; returns "<bid>2</bid>" ; or perhaps this rejoin ["<bid>" price </bid>] ; returns "<bid>2</bid>" This will have the desired result, I think Brian Hawley

 [4/9] from: gjones05:mail:orion at: 30-May-2001 18:07


Excellent explanation, Joel. Now I think I "get it." (By jove, I think he's got it!) You've both given me a fish to eat for now, and, more importantly, taught me to fish for the future! Thanks! (BTW, did I put enough exclamations points?!!!) --Scott Jones !

 [5/9] from: agem:crosswinds at: 31-May-2001 0:05


>>>>>>>>>>>>>>>>>> Ursprüngliche Nachricht <<<<<<<<<<<<<<<<<<
Am 30.05.01, 22:26:57, schrieb "GS Jones" <[gjones05--mail--orion--org]> zum Thema [REBOL] Curious 'rejoin behavior:
> I was working through some xml to rebol conversions, and I noticed
what
> seems to be curious behavior on the part of 'rejoin. I suspect that > this is a bug, but maybe I have misunderstood the behavior of this
word.
> example: > price: 2 > rejoin [ <bid> price </bid> ] ; returns: <bid2</bid>> > ; shouldn't it be: <bid>2</bid>
price: 2 rejoin [ "" <bid> price </bid> ] note the »« rejoin takes the type of the first element. You start with a tag ;-) rejoin[<a href= > http://xyz " target=somewhere"] could have uses?
> Bug or have I misunderstood some fundamental property of 'rejoin?
:-) ;-)
> --Scott Jones
-Volker

 [6/9] from: brett:codeconscious at: 31-May-2001 9:09


Hi Scott,
>> series? <test>
== true
>> append <test> "-test2"
== <test-test2>
>> source rejoin
rejoin: func [ "Reduces and joins a block of values." block [block!] "Values to reduce and join" ][ if empty? block: reduce block [return block] append either series? first block [copy first block] [ form first block] next block ] It looks odd, but is not strictly a bug. Brett.

 [7/9] from: gjones05:mail:orion at: 30-May-2001 18:25


Thanks, also Brian and Volker for the more succinct explanations and alternative routes to the desired behavior. --Scott Jones

 [8/9] from: gjones05:mail:orion at: 30-May-2001 18:31


From: "Brett Handley"
> >> source rejoin > rejoin: func [
<<quoted lines omitted: 5>>
> form first block] next block > ]
Oh, yeah, the source ... forgot about looking at the source! "Use the source, Luke." This shows the skeleton that gives the form to the explanations by Joel, Brian, and Volker. I am truly educated and enlightened at this point ... until I sleep again. :-( Thanks, Brett. --Scott Jones

 [9/9] from: carl:rebol at: 31-May-2001 0:55


Sounds like the list got you what you needed. I wanted to add one other remark... I often use tags in my scripts that build things like the REBOL.com site and docs. One technique that I've found helpful is to use the natural block combining features of REBOL. If you look at a few of my scripts, you'll find a function called: emit: func [blk] [repend html blk] with html: make string! 10000 or some such. Note that there is an implied conversion happening here. That is a very nice benefit of insert, which is under the covers of append. Then, emit [<tag> value </tag>] produces a useful result. emit [<B> "So it works well..." </B> now/date <P>] Since this simplicity has evolved over many years of using REBOL, thought that I should pass it along. Best of coding to you, -Carl

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted