Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] TECHNICAL ESSAY/CHALLENGE(s) - "Expression Based"? Re:(3)

From: joel:neely:fedex at: 3-Oct-2000 8:04

Another fine response! [Al--Bri--xtra--co--nz] wrote:
> This is a bit shorter, but still not as nice as the Rebol way as > used in System and View. > [ > 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] > > ; A better name for this function would be appreciated. > mpc: func [Block [block!]] [ > make path! compose Block > ] >
Not very inspired, but I'd suggest compose-path as the best I can come up with.
> ; A better name for this function would be appreciated. > dr: func [Block [block!]] [ > do reduce Block > ] >
Well, inspired by repend I might suggest do-re-me but then I'd have the entire von Trapp family (and Julie Andrews) accusing me of theft of "intellectual property"! ;-) Seriously, we could take inspiration from LISP and call this function apply as it allows one to apply a (dynamically determined) function to a (dynamically determined) argument list. Or perhaps eval would fit???
> use [diffs] [ > diffs: make tally [] > evens: make tally [] > odds: make tally [] > foreach num bunchanums [ > dr [mpc [diffs (either even? num ['up] ['down]) by] num] > dr [mpc [(either even? num ['evens] ['odds]) up by] num] > ] > print ["diffs/now?" diffs/now?] > print ["evens/now?" evens/now?] > print ["odds/now?" odds/now?] > ] > > ] > >> do %Tally.r
[...tracing output snipped...]
> diffs/now? 26 > evens/now? 226 > odds/now? 200 >
All told, I think the two functions above are quite nice! They certainly handle the diffs case well. I'm a little less cheerful about how they handle the evens/odds case however, in that they require that I have a NAME for an object, in order to construct a path to the needed object/method. Gabriele's do-in allows me to get at a method of an object with only the object itself (and the name of the method) in hand, not requiring a name for the object. This becomes more of an issue if we generalize my dinky example to other ways we might have worked with a collection of tally instances. Given Gabriele's do-in and my tally and data:
>> 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 + either by [n] [1]] [ down: func [/by n [number!]] [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]
== [3 1 35 8 4 5 52 42 19 13 32 43 81 2 6 34 46] ...we can say...
>> use [tallies] [
[ tallies: copy [] [ loop 3 [append tallies make tally []] [ foreach num bunchanums [ [ do-in pick tallies (num // 3 + 1) [up/by num] [ ] [ foreach tally tallies [print tally/now?] [ ] 132 212 82 ...without needing to name the members of tallies. At any rate, between you and Gabriele, we now have nicer solutions to BOTH variations of my question! Thanks!
> AllenK has a better grasp of the ideal Rebol way to do this. But > he says it is difficult to explain. More about this later, or you > could pester Allen. > :-) >
Hey, Allen! Are you there? Please chime in before Andrew makes me pester you! ;-) -jn-