Hi, Rebols,

Have a look at this, please.

Rebol [
    Title: "Curry"
    Date: 7/12/1999
    Version: 2.0.0
    File: %curry.r
    Author: "Ladislav Mecir"
    Email: [EMAIL PROTECTED]
    Comment: "Thanks, Gabriele"
]

; some helper functions to circumvent a function bug...
toblock: func [f [function!]] [append/only append/only copy [func] copy
third :f copy second :f]

reco: func [block1 [block!] block2 [block!]/local result] [
    result: copy block1
    foreach elem block2 [
        either block? :elem [
            append/only result elem
        ] [
            either function? :elem [
                append result toblock :elem
            ] [
                append result :elem
            ]
        ]
    ]
    result
]

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

curryfirst: func [f [any-function!]] [curry :f reduce [first first :f]]

;**********end of %curry.r

f: func [x y] [reduce [x y]]
cfx: curryfirst :f

>> f1: cfx 1
>> f2: cfx 2
>> f1 2
== [1 2]
>> f1 3
== [1 3]
>> f1 4
== [1 4]
>> f2 1
== [2 1]
>> f2 2
== [2 2]

Ladislav


Reply via email to