shifting characters in rejoin
[1/7] from: rchristiansen:pop:isdfa:sei-it at: 4-Oct-2000 17:44
I have created a function which returns something strange when I use
rejoin.
The function is called build-html.r which builds a markup statement.
Here is the console result. Notice the closing bracket for the opening
tag is strangely moved to the end of the rejoined string:
>> do %build-html.r
Script: "Untitled" (none)
== <font class="author"Ryan C. Christiansen</font>>
>>
Your input would be helpful. -Ryan
Here is the script:
REBOL []
author: make object! [
name: "Ryan C. Christiansen"
email-address: [rchristiansen--sei-it--com]
stylesheet-info: make object! [
tag-style: "font"
class-name: "author"
]
]
build-html: func [
"Build an HTML page element including the data to be displayed
and the markup language surrounding the data."
data [object!] "the data to be marked up"
][
open-tag: build-tag [(data/stylesheet-info/tag-style) class
(data/stylesheet-info/class-name)]
close-tag: rejoin [{</} data/stylesheet-info/tag-style {>}]
markup-string: rejoin [open-tag data/name close-tag]
markup-string
]
build-html author
[2/7] from: al:bri:xtra at: 4-Oct-2000 17:08
> I have created a function which returns something strange when I use
rejoin.
> The function is called build-html.r which builds a markup statement. Here
is the console result. Notice the closing bracket for the opening tag is
strangely moved to the end of the rejoined string:
> >> do %build-html.r
> Script: "Untitled" (none)
> == <font class="author"Ryan C. Christiansen</font>>
You've run into the interesting way that 'join works on tag!.
>> rejoin [<font> "class=" "Ryan C. Christiansen" </font>]
== <fontclass=Ryan C. Christiansen</font>>
Basically you have to convert the tags to strings to work as you'd expect,
as 'join on a tag! will put the stuff in the block inside the tag.
Something like this from my HTML! component, soon to be released on RebMail
list, and my Web Dialect list:
A: func [File [file! url!] Link [string!] /Top] [
rejoin ["" join <a> [{ href="} Encode File {"} either Top [{
TARGET="_top"}] [""]] Link </a>]
]
Which from script like this:
H1 A URL/Absolute Spectrum-Studios Name Spectrum-Studios
generates HTML like this:
<h1><a
href="http://members.nbci.com/SpectrumStudios/Spectrum Studios.html">Spect
rum Studios</a></h1>
The above line is broken by the email client I'm using.
Note that 'Spectrum-Studios is a object like:
Spectrum-Studios: Make-Page "Spectrum Studios" %"Spectrum Studios.html" [
Title Name Spectrum-Studios
Body
<snip!>
I hope that helps!
Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/
http://members.xoom.com/AndrewMartin/
[3/7] from: al:bri:xtra at: 4-Oct-2000 17:21
> You've run into the interesting way that 'join works on tag!.
>
> >> rejoin [<font> "class=" "Ryan C. Christiansen" </font>]
> == <fontclass=Ryan C. Christiansen</font>>
>
> Basically you have to convert the tags to strings to work as you'd expect,
as 'join on a tag! will put the stuff in the block inside the tag.
I forgot to mention that the end result of 'join and 'rejoin is to make the
result the same datatype! as the first datatype in the block for 'rejoin or
first argument of 'join.
>> type? rejoin [<font> "class=" "Ryan C. Christiansen" </font>]
== tag!
What's the type! of this one expression?
<font class="Ryan C. Christiansen"> </font>
;-)
Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/
http://members.xoom.com/AndrewMartin/
[4/7] from: kgd03011:nifty:ne:jp at: 5-Oct-2000 10:16
Hi Ryan,
Andrew Martin already answered you, but maybe this provides a little
different perspective ...
The results look strange because the < and > aren't part of the tag!
data, they just delimit it.
>> length? <TAG>
== 3
When you do
markup-string: rejoin [open-tag data/name close-tag]
the first item in REJOIN's block argument is a tag, so the result is
a tag. But before close-tag is appended, it is formed to a string,
which causes the delimiting < > to actually become characters of data.
A tag! is meant to represent only one tag. If you want to have more than one
tag in an HTML element, you need to make a string. So it's very easy to
correct your function. Replace the line quoted above with:
markup-string: rejoin ["" open-tag data/name close-tag]
Eric
[5/7] from: norsepower::uswest::net at: 4-Oct-2000 22:14
Thanks, guys, for the answer. I didn't think about there being a datatyping
problem, but that makes perfect sense now.
[6/7] from: chris:double:tab at: 6-Oct-2000 10:12
> The next one (I can't recall if there's a common name for it) can be
> used for statistics and other fun things:
In Lisp this function is still MAP. MAP takes any number of sequences
and the function takes a number of arguments equal to the number of
sequences:
(map 'vector #'max '(4 7 16) '(9 5 15))
=> '(9 7 16)
Perhaps the REBOL map implementation could do the same.
Chris.
--
http://www.double.co.nz/cl
http://www.double.co.nz/dylan
[7/7] from: joel:neely:fedex at: 6-Oct-2000 1:05
Hi, Chris,
Actually, it depends on which Lisp...
According to the LISP 1.5 Programmer's Manual, page 60,
Table Building and Table Reference Functions
pair[x;y] : SUBR
The function pair has as value the list of pairs of
corresponding elements of the lists x and y. The arguments
x and y must be lists of the same number of elements. ...
Of course, that manual has serious grey hair! The preface is dated
17 Aug 1962 (although my copy is from the 6th printing, April 1972).
Actually, if I were going to go by that ancient manual, I'd have to
call the apply-function-across-list operator maplist , because the
name map was given to a side-effects-only version that always
returned nil .
(OK, I couldn't help but throw in a little archaeology ;-)
Actually, I had a more pragmatic reason for ducking the multiple-
lists-as-arguments issue -- REBOL syntax!
[chris--double--tab--co--nz] wrote:
> In Lisp this function is still MAP. MAP takes any number of
> sequences and the function takes a number of arguments equal
> to the number of sequences:
>
> (map 'vector #'max '(4 7 16) '(9 5 15))
> => '(9 7 16)
>
> Perhaps the REBOL map implementation could do the same.
>
As Lisp explicitly delimits each function application, it's easy to
tell how many lists are being supplied as arguments to map (as in
your example). The only reliable way I know in REBOL to pass a
varying number of arguments is to put them in a block...
map [ [...] [...] [...] ] some-function
...but I was hesitant to do that for a few reasons.
First of all, I'd like to be able to do things like these:
>> accum: func [a [any-type!] b [any-block!] f [any-function!]][
[ foreach c b [a: f a c]
[ a
[ ]
>> map [[1 2 1] [2 3 3] [3 6 5]] func [b [block!]] [
[ append b (accum 0 b :+) / (length? b)
[ ]
== [[1 2 1 1.33333333333333] [2 3 3 2.66666666666667] [3 6 5
4.66666666666667]]
...or...
>> namer: make object! [
[ curr: #"a"
[ next: func [/local r] [
[ r: curr curr: curr + 1 r
[ ]
[ ]
>> map [[1 2 1] [2 3 3] [3 6 5]] func [b [block!]] [
[ head insert head copy b to-string namer/next
[ ]
== [["a" 1 2 1] ["b" 2 3 3] ["c" 3 6 5]]
where the function is to be applied to each block that is an
element of the (single!) argument block.
Second, I'd thought about (although I didn't include it in my
earlier posts) adding a /deep refinement by analogy with a
similar one for copy (etc.) that would recur(se) on any
nested blocks.
Finally, I'd considered handling the structure-traversing issues
separately from the data-manipulation issues, by defining (this
version is VERY quick and dirty):
>> transpose: function [b [block!]] [w i s r] [
[ r: make block! w: accum length? b/1 map b :length? :min
[ repeat i w [
[ s: make block! length? b
[ foreach c b [append/only s c/:i]
[ append/only r s
[ ]
[ r
[ ]
>> transpose [[1 2 3 4 5] ["a" "b" "c" "d" "e"]]
== [[1 "a"] [2 "b"] [3 "c"] [4 "d"] [5 "e"]]
>> transpose [[1 2 3] [14 15 16] [27 28 29]]
== [[1 14 27] [2 15 28] [3 16 29]]
>> my-scores: [4 7 16 2 12 13]
== [4 7 16 2 12 13]
>> your-scores: [9 5 15 1 15 15]
== [9 5 15 1 15 15]
>> games: transpose reduce [my-scores your-scores]
== [[4 9] [7 5] [16 15] [2 1] [12 15] [13 15]]
...and so forth.
Critiques/comments/debugging welcome!
-jn-