[REBOL] Passing refinements to sub functions. Re:(2)

2000-08-20 Thread lmecir

Hi,

back from holiday.
You might not notice, but Refined is a function defined in
www.rebol.org/advanced/highfun.r
It was my reaction to Michael's problem, but he pointed out, that
he didn't want to use it because of its overhead...

Regards
Ladislav

> Rooting around Rebol sources as the official guide book
recommends, I came
> across the following function (which would make my /refinements
refinement
> unnecessary. Now to get it to work
>
> >> help refined
> USAGE:
> REFINED f refinements
>
> DESCRIPTION:
>  Create a function doing the same thing as a function with
given
> refinements does
>  REFINED is a function value.
>
> ARGUMENTS:
>  f -- (Type: any-function)
>  refinements -- (Type: block)
>
> (SPECIAL ATTRIBUTES)
>  catch
>
>
> - Original Message -
> From: "Brett Handley" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Saturday, August 12, 2000 7:20 PM
> Subject: Re: [REBOL] Passing refinements to sub functions. Re:
>
>
> > Thanks for your response Michael.
> >
> > Now that's an interesting idea.
> >
> > I think your approach is good at getting the refinements of
the
> > higher-function processed as a set, but it obscures what
happens after
> that.
> >
> > My latest approach has been to use an additional refinement on
the
> > sub-function called /refinements. This has an argument of type
block that
> > contains pairs of refinement-name and refinement-value.
> >
> > Inspired by your work I produced this.
> >
> > main-func: function [/va /vb /vc][this-code refine-values r][
> >  sub-func/refinements refinements-to-block :main-func va
> > ]
> >
> > sub-func: function [/refinements refine-list][va vb vc][
> > do bind refine-list 'va
> > print ["va:" va]
> > print ["vb:" vb]
> > print ["vc:" vc]
> > ]
> >
> > refinements-to-block: function [
> > hi-func
> > 'sample-refinement
> > ][refinement-block][
> > refinement-block: copy []
> > foreach r first :hi-func [
> > if (refinement? r) and (r <> /local) [
> > append refinement-block to-set-word r
> > append/only refinement-block to-paren bind reduce
[to-word r]
> > :sample-refinement
> > ]
> > ]
> > compose refinement-block
> > ]
> >
> > It results in this:
> >
> > >> main-func/vb/vc
> > va: none
> > vb: true
> > vc: true
> >
> > Of course it doesn't handle any refinement arguments.
> >
> > Brett.
> >
> > - Original Message -
> > From: <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Wednesday, August 09, 2000 1:35 AM
> > Subject: [REBOL] Passing refinements to sub functions. Re:
> >
> >
> > >
> > > Well since I know you're thinking about asking for the
solution I had
> come
> > > up with, I'll go all out and post it here. The actual
solution to loop
> > > through the function items (refinements) is not much code,
but is
> tedious
> > > and messy-looking. I feel like I'm going through alot of
trouble to hide
> > > this and save a few characters in the "high-level" function,
but the
> > result
> > > is a little easier on the eye and made me learn about
contexts and
> 'bind.
> > >
> > > ; Say you have a high-level function (main-func) with a list
of
> > > refinements, and you want to pass all of these refinements
to a
> > lower-level
> > > function (sub-func). The bare parameter-passing code is:
> > >
> > > main-func: function [/va /vb /vc][this-code refine-values
r][
> > >  this-code: make-code "main-func" "sub-func"
> > >  bind this-code 'some-global bind this-code
'refine-values
> > >  do this-code
> > > ]
> > >
> > > sub-func: function [param-list][va vb vc][
> > >  set [va vb vc] param-list
> > >  print ["va:" va]
> > >  print ["vb:" vb]
> > >  print ["vc:" vc]
> > > ]
> > >
> > > ; Resulting in:
> > > >> main-func/va/vc
> > > va: true
> > > vb: none
> > > vc: true
> > >
> > > ; To support this you must define the following globally:
> > >
> > > some-global: none
> > > make-code: function [hi-func [string!] lo-func
[string!]][code][
> > >  code: copy {
> > >   refine-values: make block! []
> > >   foreach r first :hi-func [
> > >if (refinement? r) and (r <> /local) [
> > > append refine-values get bind to-word r
> 'refine-values
> > >]
> > >   ]
> > >   lo-func refine-values
> > >  }
> > >  replace/all code "hi-func" hi-func
> > >  replace/all code "lo-func" lo-func
> > >  return(to-block code)
> > > ]
> > >
> > > - Michael Jelinek
> > >
> >
> >
>
>




[REBOL] Passing refinements to sub functions. Re:(2)

2000-08-12 Thread bhandley

Thanks for your response Michael.

Now that's an interesting idea.

I think your approach is good at getting the refinements of the
higher-function processed as a set, but it obscures what happens after that.

My latest approach has been to use an additional refinement on the
sub-function called /refinements. This has an argument of type block that
contains pairs of refinement-name and refinement-value.

Inspired by your work I produced this.

main-func: function [/va /vb /vc][this-code refine-values r][
 sub-func/refinements refinements-to-block :main-func va
]

sub-func: function [/refinements refine-list][va vb vc][
do bind refine-list 'va
print ["va:" va]
print ["vb:" vb]
print ["vc:" vc]
]

refinements-to-block: function [
hi-func
'sample-refinement
][refinement-block][
refinement-block: copy []
foreach r first :hi-func [
if (refinement? r) and (r <> /local) [
append refinement-block to-set-word r
append/only refinement-block to-paren bind reduce [to-word r]
:sample-refinement
]
]
compose refinement-block
]

It results in this:

>> main-func/vb/vc
va: none
vb: true
vc: true

Of course it doesn't handle any refinement arguments.

Brett.

- Original Message -
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, August 09, 2000 1:35 AM
Subject: [REBOL] Passing refinements to sub functions. Re:


>
> Well since I know you're thinking about asking for the solution I had come
> up with, I'll go all out and post it here. The actual solution to loop
> through the function items (refinements) is not much code, but is tedious
> and messy-looking. I feel like I'm going through alot of trouble to hide
> this and save a few characters in the "high-level" function, but the
result
> is a little easier on the eye and made me learn about contexts and 'bind.
>
> ; Say you have a high-level function (main-func) with a list of
> refinements, and you want to pass all of these refinements to a
lower-level
> function (sub-func). The bare parameter-passing code is:
>
> main-func: function [/va /vb /vc][this-code refine-values r][
>  this-code: make-code "main-func" "sub-func"
>  bind this-code 'some-global bind this-code 'refine-values
>  do this-code
> ]
>
> sub-func: function [param-list][va vb vc][
>  set [va vb vc] param-list
>  print ["va:" va]
>  print ["vb:" vb]
>  print ["vc:" vc]
> ]
>
> ; Resulting in:
> >> main-func/va/vc
> va: true
> vb: none
> vc: true
>
> ; To support this you must define the following globally:
>
> some-global: none
> make-code: function [hi-func [string!] lo-func [string!]][code][
>  code: copy {
>   refine-values: make block! []
>   foreach r first :hi-func [
>if (refinement? r) and (r <> /local) [
> append refine-values get bind to-word r 'refine-values
>]
>   ]
>   lo-func refine-values
>  }
>  replace/all code "hi-func" hi-func
>  replace/all code "lo-func" lo-func
>  return(to-block code)
> ]
>
> - Michael Jelinek
>