[REBOL] TECHNICAL ESSAY/CHALLENGE(s) - "Expression Based"? Re:
From: al:bri:xtra at: 2-Oct-2000 23:10
> What would be nice here would be to invoke a method call against an object
which is the value of AN EXPRESSION...
> (*WARNING* *HYPOTHETICAL SYNTAX FOLLOWS*)
>
> use [evens odds] [
> evens: make tally []
> odds: make tally []
> foreach num bunchanums [
> (either even? num [evens] [odds])/up/by num
> ]
> print ["evens:" evens/now? " odds:" odds/now?]
> ]
>
> ...but, alas, that isn't REBOL anymore!
I believe this is called "continuation". This is present in Scheme, I
believe and it's quite nice. I believe "continuations" were in an earlier
version of Rebol at one time, but was dropped in subsequent versions.
I agree this would be very nice to have in Rebol.
> ...but these still seem awkward and unnatural, IMHO. What we're really
trying to express is simply...
> (*WARNING* *HYPOTHETICAL SYNTAX FOLLOWS*)
>
> use [diffs] [
> diffs: make tally []
> foreach num bunchanums [
> diffs/(either even? num [up] [down])/by num
> ]
> print ["net sum:" diffs/now?]
> ]
>
> ...give or take a couple of ticks. ;-)
Would this do?
[
Rebol [
File: %Tally.r
]
tally: make object! [
tot: 0
zero: does [tot: 0]
up: func [/by n [number!]] [print "up!" tot: tot + either by [n] [1]]
down: func [/by n [number!]] [print "down!" tot: tot - either by [n][1]]
now?: does [tot]
]
bunchanums: [3 1 35 8 4 5 52 42 19 13 32 43 81 2 6 34 46]
use [diffs] [
diffs: make tally []
foreach num bunchanums [
do probe reduce [
make path! compose [
diffs (either even? num ['up] ['down]) by
] num
]
]
diffs/now?
]
]
>> do %tally.r
[diffs/down/by 3]
down!
[diffs/down/by 1]
down!
[diffs/down/by 35]
down!
[diffs/up/by 8]
up!
[diffs/up/by 4]
up!
[diffs/down/by 5]
down!
[diffs/up/by 52]
up!
[diffs/up/by 42]
up!
[diffs/down/by 19]
down!
[diffs/down/by 13]
down!
[diffs/up/by 32]
up!
[diffs/down/by 43]
down!
[diffs/down/by 81]
down!
[diffs/up/by 2]
up!
[diffs/up/by 6]
up!
[diffs/up/by 34]
up!
[diffs/up/by 46]
up!
== 26
This should solve the other challenges as well. Just shift around the
parenthesis to suit.
Andrew Martin
And,... I even had time to eat dinner! :-)
Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/
http://members.xoom.com/AndrewMartin/