[REBOL] TECHNICAL ESSAY/CHALLENGE(s) - "Expression Based"? Re:(2)
From: joel:neely:fedex at: 3-Oct-2000 7:33
[giesse--dsiaq1--ing--univaq--it] wrote:
> >> 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
>
Excellent! (I also like the rewrite of the final report!)
> > 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
>
Well, this works, but notice that the "/by num" part still has
to be duplicated between the alternatives. Do you have a way
around that?
> (using COMPOSE here you may get closer to your hypothetical
> syntax...)
>
Being a Bear of Small Brain (and I was up late last night and
haven't had sufficient coffee yet this morning), I must ask for
an example of what you had in mind here.
Thanks!
-jn-