Elegant way to reference a function?
[1/10] from: pat665::ifrance::com at: 3-Dec-2001 22:44
Hi all,
What is the elegant way to reference a function?
Let me explain a little bit my context :
I am working on a program that selects items using criterion (may be the plural criteria
is required here, but I'm french you know). Theses items are photos, for example, or
texts such as FAQ.
For photos, the person who is on the photo is a criterion. To select photos, it is just
a matter of a click on a checkbox. With this, it is easy to see photos with marc and
chouchou (marc is my son, chouchou is my cat). It is possible to make exclusion, for
a FAQ program, it will be possible to search for FAQ with 'bind and 'use but not with
'view.
Back to the question : I don't want the main program to know about photos, or FAQ or
anything else. However it's not the same to show a photo or to display a FAQ. So I need
a generic function, used during the test, that will be replaced with minimum effort (I'm
actually from the south of france ;-) with the required function.
To be more practical, if I have a button like this on a window.
button "show me" [display]
I want the function display to be :
- a simple test function when debuging or
- a function that shows a photo or
- a function that displays a FAQ text
I have thought of aliases, but there are a bit problematic to me (because I don't know
to get rid of them).
I think may be it is as simple as :
display: :my-show-photo-function
Any idea, or suggestions ?
Patrick
[2/10] from: lmecir:mbox:vol:cz at: 4-Dec-2001 8:46
Hi Patrick,
your solution (display: :my-show-photo-function) seems to be perfect. Do you
have any problem with that? Aliases aren't well suited, they are more
permanent, than you might need.
----- Original Message -----
From: "Patrick Philipot" <[pat665--ifrance--com]>
To: <[rebol-list--rebol--com]>
Sent: Monday, December 03, 2001 10:44 PM
Subject: [REBOL] Elegant way to reference a function?
Hi all,
What is the elegant way to reference a function?
Let me explain a little bit my context :
I am working on a program that selects items using criterion (may be the
plural criteria is required here, but I'm french you know). Theses items are
photos, for example, or texts such as FAQ.
For photos, the person who is on the photo is a criterion. To select photos,
it is just a matter of a click on a checkbox. With this, it is easy to see
photos with marc and chouchou (marc is my son, chouchou is my cat). It is
possible to make exclusion, for a FAQ program, it will be possible to search
for FAQ with 'bind and 'use but not with 'view.
Back to the question : I don't want the main program to know about photos,
or FAQ or anything else. However it's not the same to show a photo or to
display a FAQ. So I need a generic function, used during the test, that will
be replaced with minimum effort (I'm actually from the south of france ;-)
with the required function.
To be more practical, if I have a button like this on a window.
button "show me" [display]
I want the function display to be :
- a simple test function when debuging or
- a function that shows a photo or
- a function that displays a FAQ text
I have thought of aliases, but there are a bit problematic to me (because I
don't know to get rid of them).
I think may be it is as simple as :
display: :my-show-photo-function
Any idea, or suggestions ?
Patrick
[3/10] from: chaz:innocent at: 3-Dec-2001 11:45
This sounds like a context thing.
----- Original Message -----
From: "Patrick Philipot" <[pat665--ifrance--com]>
To: <[rebol-list--rebol--com]>
Sent: Monday, December 03, 2001 1:44 PM
Subject: [REBOL] Elegant way to reference a function?
> Hi all,
>
> What is the elegant way to reference a function?
>
> Let me explain a little bit my context :
>
> I am working on a program that selects items using criterion (may be the
plural criteria is required here, but I'm french you know). Theses items are
photos, for example, or texts such as FAQ.
> For photos, the person who is on the photo is a criterion. To select
photos, it is just a matter of a click on a checkbox. With this, it is easy
to see photos with marc and chouchou (marc is my son, chouchou is my cat).
It is possible to make exclusion, for a FAQ program, it will be possible to
search for FAQ with 'bind and 'use but not with 'view.
> Back to the question : I don't want the main program to know about photos,
or FAQ or anything else. However it's not the same to show a photo or to
display a FAQ. So I need a generic function, used during the test, that will
be replaced with minimum effort (I'm actually from the south of france ;-)
with the required function.
[4/10] from: al:bri:xtra at: 4-Dec-2001 21:21
> What is the elegant way to reference a function?
> I think may be it is as simple as :
>
> display: :my-show-photo-function
That's exactly right.
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[5/10] from: rebol665:ifrance at: 4-Dec-2001 13:14
Thanks to Andrew and Ladislav,
So it will be a <<display: :my-show-photo-function>> solution !
Patrick
[6/10] from: rebol665:ifrance at: 4-Dec-2001 13:19
Hi Chaz,
I did not know what a context was so I went for the doc. It reads "This is a
shortcut for MAKE OBJECT!. ". So a context is an other name for an object. I
'm afraid I don't understand how it will help me. Do you have more on this ?
Thanks
Patrick
[7/10] from: joel:neely:fedex at: 4-Dec-2001 8:50
Hi, Patrick,
pat665 wrote:
> Hi Chaz,
>
> I did not know what a context was so I went for the doc. It
> reads "This is a shortcut for MAKE OBJECT!. ". So a context
> is an other name for an object. I'm afraid I don't understand
> how it will help me. Do you have more on this ?
>
Unfortunately the context to Chaz referred is not the CONTEXT
word pre-defined to the interpreter.
curmudgeonly-opinion: {
Of all the examples of nearly-useless redundant words,
creating a "shortcut" for the two-word phrase MAKE OBJECT!
(thus saving a whole 4 keystrokes!) is The Worst Example
of A Bad Idea, most especially because it overloads what
is already a barely-documented concept central to
understanding REBOL.
}
Here's the meaning of "context" (the idea, not the so-called
shortcut...) by comparison/contrast with other languages:
In many programming languages, variables are just names for
addresses in memory into which one may store data. The job
of the compiler was to generate code by translating variable
names into the corresponding memory addresses.
Once we programmers started breaking programs into idea-sized
chunks (such as FORTRAN subroutines -- this is a very old
issue in programming ;-), we had to deal with the idea
of the "scope" of a variable, which was the portion of the
source code within which a particular name had a particular
address associated with it. Languages such as C are the
endpoint of that evolutionary path, with various keywords
to control the visibility and lifetime of these names and
the associated memory addresses.
In a radical departure from this model, LISP introduced the
idea that symbols/names in a program were more like words
in a dictionary; to find the value associated with a name,
you look that name up in an appropriate dictionary and take
the corresponding value found there. (Most of the LISP
literature calls these dictionaries "environments".)
This opened the possibility that there could be multiple
dictionaries, with one or more of them being relevant for
a particular range of time or code.
A REBOL "context" is such a dictionary, and there are
several features of REBOL which cause contexts to be
created: defining a function, evaluating a USE expression,
and making an object all do that.
Consider this example:
>> generate-pairs: func [n [integer!] /local pair-with result] [
[ result: clear []
[ pair-with: func [m [integer!] n [integer!] /local result] [
[ result: clear []
[ repeat i n [repend/only result [m i]]
[ ]
[ repeat i n [append result pair-with i n]
[ ]
>> result: "this is some other thing"
== "this is some other thing"
>> generate-pairs 3
== [[1 1] [1 2] [1 3] [2 1] [2 2] [2 3] [3 1] [3 2] [3 3]]
>> result
== "this is some other thing"
(not written for maximum efficiency, but to make a point)
The word RESULT appears many times in the above transcript, but
in one sense there are three words named "result". There's a
global context which includes the following mapping
'result --> "this is some other thing"
a context associated with GENERATE-PAIRS which includes (at the
end) the mappings
'result --> [[1 1][1 2][1 3][2 1][2 2][2 3][3 1][3 2][3 3]]
'pair-with --> (...a function value...)
and a context associated with that last function value above which
includes (at the end)
'result --> [[3 1][3 2][3 3]]
These hidden dictionaries are central to the behavior of REBOL,
but their nature is for the most part undocumented. About the
only semi-explicit appearance they make in REBOL is through the
BIND native, which causes one or more words to become associated
with a different context, based on a word from that context.
Evaluating a REBOL word doesn't mean looking at a corresponding
memory address fixed at compile time. Instead it means checking
the context to which that word is bound, and seeing what value
is associated with that word in the context "dictionary". (And
there's more that may happen, depending on the type of the value
that is found there, but that's a different discussion.)
AFAICT (and there are many wizards on this list who will correct
me if I'm wrong), one can imagine something like this:
'generate-pairs ---+
|
V (global context)
+----------------+---+
: ... : :
'result ----> +----------------+---+
| result | *------> "this is some other thing"
+----------------+---+
| generate-pairs | *------> +-----------
+----------------+---+ | ... whatever a function!
: ... : : | looks like inside the
+----------------+---+ | REBOL interpreter...
| *
+-|---------
|
+--------------------------+
'pair-with ---+ |
| |
V V (context for GENERATE-PAIRS)
'result ----> +-----------+---+
| result | *------> [[1 1]...]
+-----------+---+
| pair-with | * |
+-----------+-|-+
|
V
+-----
| ...whatever a function!
| looks like inside the
| REBOL interpreter...
| *
+-|---
V
... you get the idea...
What value you get for the symbol 'result depends on which
dictionary you look it up in.
Finally, since an object has a context, somebody decided that
the word CONTEXT could be used as a "shortcut" for the two-word
phrase MAKE OBJECT! thus providing an opportunity to confuse the
two meanings.
HTH!
-jn-
--
; sub REBOL {}; sub head ($) {@_[0]}
REBOL []
# despam: func [e] [replace replace/all e ":" "." "#" "@"]
; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"}
print head reverse despam "moc:xedef#yleen:leoj" ;
[8/10] from: rebol665:ifrance at: 4-Dec-2001 20:15
Hi Joel
Thank you for this valuable information. I printed it and I am going to read
it again and experiment with it ASAP. The good spirit and the high level of
this rebol-list amazes me every day !
Patrick
[9/10] from: greggirwin:mindspring at: 4-Dec-2001 13:50
Excellent post Joel!
I've been searching for an analogy to wrap my poor brain around so I can
visualize these things. If nobody (RT or otherwise) says it's incorrect, I'm
going to use your dictionary analogy.
Thanks!
--Gregg
[10/10] from: rpgwriter:yaho:o at: 4-Dec-2001 15:12
--- Patrick Philipot <[pat665--ifrance--com]> wrote:
> To be more practical, if I have a button like this
> on a window.
<<quoted lines omitted: 3>>
> - a function that shows a photo or
> - a function that displays a FAQ text
How about using something like:
button "show me" [thing/display]
Where "thing" is an object which wraps the
default object used in debugging, photo,
or faq text, and includes a "display" method
as part of the object definition?
Chris Dicely
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted