Hi,

I recently suggested my Rt (Referentially transparent) function, but
overlooked, that Joel wanted to have the first element of the path to be an
object (or a general Rebol value), not a Rebol word. Here is my next trial:

rt: func [
    {
        a referentially transparent way
        to invoke a path on a given object/function/something
        Usage:
            rt [[object 'function 'refinement1 ...] arg1...]
    }
    [catch]
    blk [block!]
] [
    do function [] [result1 result2 something] [
        throw-on-error [
            result1: do/next first blk
            something: first result1
            result2: append copy ['something] reduce second result1
            do head change/only copy blk to path! result2
        ]
    ]
]
; Warning - although it seems to work, it is untested against the GC bug!
; (and my personal guess is, that it wouldn't survive)

; Joel's examples:
bunchanums: [3 1 35 8 4 5 52 42 19 13 32 43 81 2 6 34 46]

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]
]

; example #1:
use [evens odds] [
    evens: make tally []
    odds: make tally []
    foreach num bunchanums [
        rt [[either even? num [evens] [odds] 'up 'by] num]
    ]
    print ["evens:" evens/now? " odds:" odds/now?]
]

{
    Result:
evens: 226  odds: 200
}

; example #2:
use [diffs] [
    diffs: make tally []
    foreach num bunchanums [
        rt [[diffs either even? num ['up] ['down] 'by] num]
    ]
    print ["net sum:" diffs/now?]
]

{
    Result:
net sum: 26
}

Regards
    Ladislav

Reply via email to