[REBOL] TECHNICAL ESSAY/CHALLENGE(s) - "Expression Based"? Re:
From: g:santilli:tiscalinet:it at: 3-Oct-2000 10:19
[joel--neely--fedex--com] wrote:
> CHALLENGE #1:
>
> Given an object (which is determined as the value of an expression,
> not by having "it's name" in hand), how can we call a predetermined
> method of that object, complete with argument(s) and refinement(s)?
> In other words, the object may vary, but the function and arguments
> will not.
>> do-in: func [obj [object!] code [block!]] [do bind/copy code in obj 'self]
>> tally: make object! [
[ tot: 0
[ zero: does [tot: 0]
[ up: func [/by n [number!]] [tot: tot + any [n 1]]
[ down: func [/by n [number!]] [tot: tot - any [n 1]]
[ now?: does [tot]
[ ]
>> bunchanums: [3 1 35 8 4 5 52 42 19 13 32 43 81 2 6 34 46]
== [3 1 35 8 4 5 52 42 19 13 32 43 81 2 6 34 46]
>> use [evens odds] [
[ evens: make tally []
[ odds: make tally []
[ foreach num bunchanums [
[ do-in either even? num [evens] [odds] [up/by num]
[ ]
[ foreach tally [evens odds] [prin [tally ":" do-in get tally
[now?] " "]]
[ prin newline
[ ]
evens : 226 odds : 200
> CHALLENGE #2:
>
> Given a specific object in hand, how can we invoke one of its
> methods (which one is determined by an evaluation) with a given
> list of arguement(s)/refinement(s)? In other words, the choice
> of function is varying, but the object and arguments are not.
>> use [diffs] [
[ diffs: make tally []
[ foreach num bunchanums [
[ do-in diffs either even? num [[up/by num]] [[down/by
num]]
[ ]
[ print ["net sum:" diffs/now?]
[ ]
net sum: 26
(using COMPOSE here you may get closer to your hypothetical
syntax...)
Doing-in,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/