[REBOL] Re: Binding
From: lmecir:mbox:vol:cz at: 19-Nov-2002 12:57
Hi Robert,
> > foreach [id object] list [
> > if do bind predicate 'id [do bind action 'id]
> > ]
> Hi, I wanted to avoid to bring BIND into the loop.
yes, it is advisable to avoid bringing BIND into the loop, that is why the
compose/reduce alternative surely looks better
> So, it really seems
> to be that bind is one-shot action.
I am not sure I understand what you mean here...
> Every word that get's bound to the
> local context after BIND isn't accessible from the bounded block.
Again, I don't think, I understand...
> IMO it must be harder to keep
> track of the words that the bounded block can access
No such information exists. A block can contain words. Every word in a block
has its own binding attribute.
> > foreach [id object] list compose/deep [
> > if do (predicate) [do (action)]
> > ]
I think, that it may contain a bug. The correct way might be:
foreach [id object] list compose/deep [
if do [(predicate)] [(action)]
]
> Why does compose work here? Will it execute an implicit BIND?
No. FOREACH binds its BODY argument appropriately. Unfortunately, if you use
variables 'predicate and 'action, FOREACH binds only the variables instead
of the values they represent.
The code containing COMPOSE doesn't allow FOREACH to bind the variables,
because it replaces the variables by their contents before BODY is supplied
to FOREACH, and that is why FOREACH is allowed to do what is needed.
> Would it work
> with REDUCE as well?
Here is a REDUCE version:
foreach [id object] list reduce [
'if 'do predicate action
]
-L