Iterators & Functionids
[1/5] from: robert::muench::robertmuench::de at: 27-Aug-2002 11:24
Hi, I try to get the following behaviour:
test-action: [print "---" probe first iterator]
test-predicate: [print type? list]
forall-records object-list test-predicate test-action
forall-records: func [list predicate action][
do predicate ; just for simple test
; iterate through the whole list of values
; check if the first entry satisfies the predicate
; if so execute the action
forall list [if predicate [action]]
]
I want to provide a predicate and an action that can use the parameters
and local words. Of course the above code fails
** Script Error: list has no value
What I came up with is a not so nice and IMO inflexible solution:
forall-records: func [list predicate action][
if any [none? list empty? action][exit]
; iterate through the whole list of values
; check if the first entry satisfies the predicate
; if so execute the action
forall list [if (predicate first list) [do action]]
]
test-predicate: func [object][
either not none? (select object 'name) [true][false]
]
forall-records object-list :test-predicate test-action
I expect the first problem to be a binding problem. I read through the
bindung stuff provided by Ladislaw but it's really hard... So how can
the concept of C++ iterators, predicates, and functionids be implemented
in Rebol? Did someone already wrote some generic functions for this?
Robert
[2/5] from: g:santilli:tiscalinet:it at: 27-Aug-2002 12:39
Hi Robert,
On Tuesday, August 27, 2002, 11:24:11 AM, you wrote:
RMM> Hi, I try to get the following behaviour:
[...]
What about:
foreach-record: func ['word records predicate action] [
predicate: func reduce [word] predicate
action: func reduce [word] action
foreach record records [
if predicate record [action record]
]
]
>> list: [1 2 3 4 5 6 7 8 9]
== [1 2 3 4 5 6 7 8 9]
>> foreach-record num list [even? num] [print num]
2
4
6
8
== none
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[3/5] from: robert:muench:robertmuench at: 27-Aug-2002 20:20
> -----Original Message-----
> From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]
<<quoted lines omitted: 10>>
> ]
> ]
Hi, looks good to me. I came up with the following solution:
forall list [
object: first list
if (do bind copy predicate 'list) [do bind copy action
'list]
]
Within the predicate & action block you now have access to 'object.
Using bind makes it possible to get access to the local words. I use
copy to avoid destroying the global context binding of the provided
blocks. Robert
[4/5] from: g:santilli:tiscalinet:it at: 28-Aug-2002 2:13
Hi Robert,
On Tuesday, August 27, 2002, 8:20:56 PM, you wrote:
RMM> forall list [
RMM> object: first list
RMM> if (do bind copy predicate 'list) [do bind copy action
RMM> 'list]
RMM> ]
This way you are copying and rebinding the blocks at each cycle.
Using functions is probably the best way, but if you prefer blocks
you can:
predicate: bind/copy predicate 'list
action: bind/copy action 'list
forall list [
object: first list
if do predicate [do action]
]
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[5/5] from: robert:muench:robertmuench at: 29-Aug-2002 8:48
> -----Original Message-----
> From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]
<<quoted lines omitted: 5>>
> cycle. Using functions is probably the best way, but if you
> prefer blocks you can:
Hi, yes of course ;-)) It was late yesterday... Robert
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted