Hi, Rebols,

during our discussion with Elan on  preventing code duplication in objects
it seemed good to have the curried functions.

I remember that Jeff submitted something on this, but i lost it somewhere.

This is an independent solution, trying to be as complete as possible.

Enjoy the expressive power of Rebol - a gem (thanks, Carl 8^)

-Ladislav

Rebol [
    Title: "Curried"
    Date: 3/12/1999
    File: %curried.r
    Author: "Ladislav Mecir"
    Email: [EMAIL PROTECTED]
    ]

curried: func ["Create curried functions"
    fnc [any-function!] "Function to be curried"
    args [block!] "Arguments of the curried fnc"
    /local formargs restargs nonargs original
    ] [
    formargs: first :fnc
    if not empty? nonargs: difference/only args formargs [
        make error! [script expect-arg curried nonargs]
        ]
    restargs: difference/only formargs args
    original: append copy [fnc] formargs
    func args reduce ['func restargs original]
    ]

;**********end of %curried.r


some examples:

>> fun: func [a b] [a * 10 + b]
>> cfun: curried :fun [c]
** Script Error: curried expected nonargs argument of type: none.
** Where: make error! compose [script expect-arg curried nonargs]

Request #1: This shows how CURRIED handles errors (argument C is not
allowed, because it isn't between the formal args of FUN). Can somebody show
me, how to MAKE ERROR! so, that I could say which args are incorrect - those
in NONARGS).

more examples:

>> cfun: curried :fun [a]
>> source cfun
cfun: func [a][func [b] [fnc a b]]
>> dfun: cfun 1
>> source dfun
dfun: func [b][fnc a b]
>> dfun 2
== 12

Request #2: I do'nt think this function is reliable (what a pity! - Jeff,
Bo, anyone?), because when you look at source DFUN you see FNC in there, but
FNC is argument of CURRIED and valid only until you evaluate CURRIED for the
next time, due to the nonreentrant nature of function evaluation, problems
with extents, garbage collector ... So Rebol shows exceptional
expressibility, but worse implementation here...

Reply via email to