Hi,

this thread inspired me to write:

to-func: func [
    {Create a function doing the same thing as a function with
given refinements does}
    [catch]
    f [any-function!]
    refinements [block!]
    /local spec body
    f-spec mode append-mode item n holder getargs simple-spec
] [
    append-mode: [
        if not refinement? :item [
            either block? :item [
                append/only spec load mold :item
            ] [
                append/only spec :item
            ]
        ]
    ]
    spec: copy []
    f-spec: third :f
    mode: append-mode
    while [not tail? f-spec] [
        item: first f-spec
        f-spec: next f-spec
        do mode
        if refinement? :item [
            if :item = /local [break]
            item: to word! :item
            either find refinements :item [
                mode: append-mode
            ] [
                mode: []
            ]
        ]
    ]
    simple-spec: first func spec []
    holder: make object! compose/deep [f: first [(:f)]]
    body: bind compose [
        (:holder)
        (to path! append copy [f] refinements)
        (
            getargs: copy []
            foreach arg simple-spec [
                append getargs compose [get/any (arg)]
            ]
            getargs
        )
    ] in holder 'f
    func spec body
]

{
    Example:

    insert-only: to-func :insert [only]
}

Regards
    Ladislav


Reply via email to