Hi Carlos,

CL> Would it be very nice to have an AT-like native function in
CL> REBOL/Core, don't you think so?

Submit a wish request to RAMBO for a refinement on FIND. It could be
very useful at times.

I have to admit that I haven't found a deep need for it myself,
because REBOL has different ways of dealing with things, but it could
be a nice thing to make people more comfortable (and productive) when
they move to REBOL from another language.

I did the following to test the new PARSE feature that allows you to
return from a parse action, so it will only work under the newest
releases, but the idea could be adapted for the official releases.

    find-Nth: func [
        "Returns the series at occurrence N of value or none."
        series [series!]
        value
        n      [integer!]
        /local count pos
    ][
        count: 0
        parse series [
            some [
                to value pos: (
                   count: count + 1
                   if count = n [return pos]
                ) skip
            ]
        ]
        none
    ]
    test-Nth: func [ser val occ] [print mold find-Nth ser val occ]
    repeat i 6 [
        test-Nth "aaa*bbb*ccc*ddd*eee*fff" #"*" i
    ]
    test-Nth "aaa bbb ccc ddd eee fff" #"*" 2
    repeat i 5 [
        test-Nth [aaa 0 bbb 1 ccc 0.0 ddd 2 eee 1.1 fff 3] integer! i
    ]
    repeat i 3 [
        test-Nth [aaa 0 bbb 1 ccc 0.0 ddd 2 eee 1.1 fff 3] decimal! i
    ]
    repeat i 7 [
        test-Nth [aaa 0 bbb 1 ccc 0.0 ddd 2 eee 1.1 fff 3] number! i
    ]
    test-Nth [aaa 0 bbb 1 ccc 0.0 ddd 2 eee 1.1 fff 3 x] number! 7
    test-Nth [aaa 0 bbb 1 ccc 0.0 ddd 2 eee 1.1 fff 3 x] issue! 1

    
Then you can easily emulate what you want from Fox.

>> s: "aaa*bbb*ccc*ddd*eee*fff"
>> left: func [series pos] [copy/part series pos]
>> left s find-Nth s #"*" 2
== "aaa*bbb"

Or even go a step further.

>> up-to-Nth: func [series value n] [left series find-Nth series value n]
>> up-to-Nth s #"*" 2
== "aaa*bbb"

-- Gregg                         


-- 
To unsubscribe from the list, just send an email to rebol-request
at rebol.com with unsubscribe as the subject.

Reply via email to