[REBOL] Re: [refactoring s-c?]
From: lmecir:mbox:vol:cz at: 15-Mar-2002 19:15
Hi Pat,
thanks for looking into that. The goal of the USE and REDUCE usage is to
create a word equal to a given word but not the same as the given word. I
could have defined a function NONSAME as follows:
nonsame1: func [
{
create a word equal to the given word
but not the same as the given word
}
word [word!] {the given word}
] [
use reduce [word] reduce [
'first reduce [word]
]
]
My implementation was incorrect (as I have found out when I looked into it).
A correct implementation should have been as follows:
nonsame: func [
{
create a word equal to the given word
but not the same as the given word
}
word [word!] {the given word}
] [
first use reduce [word] reduce [
reduce [word]
]
]
Can you find my error?
The implementation of the S-C? function could then have been as follows:
s-c?: func [
{Are word1 and word2 bound to the same context?}
word1 [word!]
word2 [word!]
] [
found? any [
all [
undefined? word1
undefined? word2
]
all [
not undefined? word2
same? word1 bind nonsame word1 word2
]
]
]
Cheers (and thanks for your question)
Ladislav
----- Original Message -----
From: "pat665" <[rebol665--ifrance--com]>
To: <[rebol-list--rebol--com]>
Sent: Wednesday, March 13, 2002 10:43 PM
Subject: [REBOL] [refactoring s-c?]
Hi rebollers
Again exploring Ladislav's contexts.html, I have difficulty understanding
the s-c? function. The goal of the s-c? function is to test if two words are
in the same context.
Two Words WORD1 and WORD2 are bound to the same context, if the expression
(s-c? word1 word2) yields TRUE.
Here (1) is the original version of s-c? and (2) my simplified version. Both
return the same result (3). Something is certainly missing in the simplified
version, but what ? what are the purposes of the 'use and 'reduce in the
original version ?
(1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; s-c? function
undefined?: func [
{determines, if a word is undefined}
word [any-word!]
] [
error? try [error? get/any :word]
]
s-c?: func [
{Are word1 and word2 bound to the same context?}
word1 [word!]
word2 [word!]
] [
found? any [
all [
undefined? word1
undefined? word2
]
all [
not undefined? word2
same? word1 bind use reduce [word1] reduce [
'first reduce [word1]
] word2
]
]
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; the (over?) simplified version
(2)
sc: func [
{Are word1 and word2 bound to the same context?}
word1 [word!]
word2 [word!]
] [
print mold use reduce [word1] reduce ['first reduce [word1]]
found? any [
all [
undefined? word1
undefined? word2
]
all [
not undefined? word2
same? word1 first bind [word1] word2
]
]
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; tests
(3)
>> o: make object! [a: 1 b: 2]
>> a: 100
== 100
>>
>> word1: 'a
== a
>> word2: second first o
== a
>> word3: third first o
== b
The original s-c? gives :
>>s-c? word1 word2
== false
>>s-c? word1 word3
== false
>>s-c? word2 word3
== true
The simplified version gives
>>sc word1 word2
a
== false
>>sc word1 word3
a
== false
>>sc word2 word3
a
== true
As for the subject of this post, I was just kidding. I'am pretty sure that
Ladislav's code do not need any refactoring.
Patrick