[REBOL] Re: [RWEBOL] HOF and system exploration
From: jan:skibinski:sympatico:ca at: 27-Oct-2002 7:54
Ladislav, Gabriele:
Attached are some good examples, which I
hope you will find satisfactory! I discovered
pleasent trick with parens! I know now why the orginal
'similar was failing. I'll leave explanation for
later. Here is just a quick view of the results.
I just posted the script %hof-accu.r in the util section.
(High Order Funcctions - Apply/Closure/Curry/Uncurry,
HOF-ACCU). You will find the discussion there.
Few bugs and shortcuts need to be fixed.
Refinements are not treated yet.
Best regards,
Jan
========================================================
Two samples of code ('apply and 'closure) are at the bottom.
1. APPLY :: [any-function!][block!][any-type!]
2. CLOSURE :: [any-function!][paren!]
3. CURRY :: [paren!][integer!][paren!]
4. UNCURRY :: [paren!][block!][paren!]
5. SIGNATURE :: [word!][block!]
6. SIGNATURE' :: [any-function! paren!][block]
-----------------------------------------------
Using APPLY:
apply :do [:+ 1 2]
== 3
apply (func[x y][x * y + 10]) [2 3]
== 16
--------------------------------------
CLOSURE and CURRYING:
[Now you can see the code of SIMILAR-like function]
g: closure :do
:g
== (native)
g: uncurry :g [:+ 1 2]
== (native :+ 1 2)
Now that we have uncurried the g function
let us execute it:
g
== 3
And again....
g
== 3
Voila! Here you go!
---------------------------------------
PLAYING WITH CURRY/UNCURRY
Some dummy test function f of this
signature:
f :: [[money!] [date!] [integer!] [logic!]]
Making closure, fully curried:
g: closure :f
signature' :g
== [[money!] [date!] [integer!] [logic!]]
signature' (first :g) ; of function embedded f
== [[money!] [date!] [integer!] [logic!]]
And now uncurrying one by one
g: uncurry :g [$100]
signature' :g
== [[date!] [integer!] [logic!]]
g: uncurry :g [now/date 10]
signature' :g
== [[logic!]]
Now the closure can be fully evaluated by calling
it by name:
g
== false ; some dummy result
---------------------------------------------
Finally, here are two functions from %hof-accu.r
apply: func [
"Apply function :f to a block of its args"
f [any-function!]
args [block!]
/local result [any-type!] xs
][
xs: copy args
insert xs :f
result: to-paren xs
result
]
closure: func [
{Generate closure for function :f with no arguments.
Such closure corresponds to the fully curried function.}
f [any-function!]
/local result [paren!] xs
][
xs: to-block :f
result: to-paren xs
:result
]