[REBOL] Re: [refactoring s-c?]
From: rebol665:ifrance at: 17-Mar-2002 22:16
Hi Ladislav,
1. first 'first
If I understand nonsame1 is equivalent to nonsame except if called with the
word 'first.
<<yoda>>
same? 'first nonsame1 'first
** Script Error: first has no value
<<L>>
So I have tried to find out the part of nonsame1 that breaks ...
In the following line from nonsame1, I see two parts :
use reduce [word] reduce ['first reduce [word]]
or
use <part 1> <part 2>
<part 1> being a block of words, here a block with only one word.
<part 2> being a block to evaluate. Evaluation returning the given word.
A little test shows that <part 1> is correct.
>> myword: 'first
== first
>> reduce [myword]
== [first]
Therefore I concentrate myself on <part 2>. To emulate <part 2> I create
this little function
test: func [x [word!]][do reduce ['first reduce [x]]]
However I was desappointed with the result.
>> test myword
== first
No error, so neither <part 1> or <part 2> is responsible for the error. It
is only when they are bind together by use, that an error rises. I think
also there is nothing wrong with 'first by itself. To clear that, I have
made the following test where 'first is replaced by 'second.
>> test2: func [word [word!]][use reduce [word] reduce ['second reduce
[word]]]
>> test2 'second
** Script Error: second has no value
Bingo ! let's try to remove first ...
>> test2: func [word [word!]][use reduce [word] reduce [reduce [word]]]
>> test2 'first
== [first]
It is almost ok, except that a block is returned instead of a word. And it
was indeed the purpose of using 'first to give us a word. A part that it
returns a block, test2 give us the right thing : a word with the same
spelling but not the same word.
>> first test2 'first
== first
Here we can get the "new" first with the same spelling.
>> same? 'first first test2 'first
== false
And it is not the same as the global first. From that it is easy to build
test3 which is indeed nonsame :
>> test3: func [word [word!]][first use reduce [word] reduce [reduce
[word]]]
>> test3 'first
== first
>> same? 'first test3 'first
== false
2. second reduce
Let's play again with my simplification of use :
use <part 1> <part 2>
where
<part 1> is a block including one word
<part 2> is a block returning that word
Why can't we simply write :
test4: func [x [word!]][use [x][x]]
This is an answer
>> use [x][x]
** Script Error: x has no value
** Where: do-boot
** Near: x
The x in part 2 exists but have never been set. It has no value to return.
In fact, we don't want the value of x but the word associated. May be this
could work.
test4: func [x [word!]][use [x]['x]]
>> test4 'first
== x
Nope, 'x is taken litteraly here.
test4: func [x [word!]][use [x][compose [('x)]]]
>> test4 'first
== [x]
test4: func [x [word!]][use reduce [x][compose [('x)]]]
>> test 'first
== [first [first]]
Yeah, another puzzle: one in -> two out. However it is first that comes out.
So I think I understand the first reduce. Let's replace compose by reduce
too.
test4: func [x [word!]][use reduce [x][reduce [x]]]
>> test4 'first
== [first]
Not bad ! but is it the real one ?
>> same? 'first first test4 'first
== true
No luck again. Indeed it says something. Since we get the global 'first, it
means that the use did not work at all.
This one is a bit tricky
test5: func [x [word!]][use [first][reduce [x]]]
>> test5 'first
== [first]
>> same? 'first test5 'first
== false
It works, however it works only with 'first. It shows anyway that only <part
2> needs to be taken care of. Let's try to see what is wrong in that <part
2>.
test6: func [x [word!]][use reduce [x][print reduce [x]]]
>> test6 'first
** Script Error: first expected series argument of type: series pair event
money date object port
time tuple any-function library struct event
** Where: test6
** Near: first
It seems that x is evaluated again. To prevent evaluation, it can be put in
an extra block.
test6: func [x [word!]][use reduce [x][print [reduce [x]]]]
>> test6 'first
first
As we know that print reduces its argument, all we have to do is to replace
print by reduce.
test7: func [x [word!]][use reduce [x][reduce [reduce [x]]]]
>> test7 'first
== [[first]]
And yet there is an external block to get rid of.
test8: func [x [word!]][use reduce [x] reduce [reduce [x]]]
>> test8 'first
== [first]
>> same? 'first test8 'first
== false
And finally from what we learned from (1.).
test9: func [x [word!]][first use reduce [x] reduce [reduce [x]]]
>> test9 'first
== first
>> same? 'first test9 'first
== false
Which is not a surprise because test9 is nonsame.
3. third contexts.html
I was very proud to see my name in the acknowledgements part. I show it to
everybody in the house except my cat.
4. fourth unbound
I noticed the replacement of error? by any-type? I will look into later,
because I have very few little grey cells available at the moment.
Patrick