Rebol Programming/closure

From Wikibooks, open books for an open world
< Rebol Programming
Jump to navigation Jump to search

USAGE:[edit | edit source]

CLOSURE spec body 

DESCRIPTION:[edit | edit source]

Defines a closure function.

CLOSURE is a function value.

ARGUMENTS[edit | edit source]

  • spec -- Help string (opt) followed by arg words (and opt type and string) (Type: block)
  • body -- The body block of the function (Type: block)

(SPECIAL ATTRIBUTES)[edit | edit source]

  • catch

SOURCE CODE[edit | edit source]

closure: func [
    "Defines a closure function." 
    spec [block!] {Help string (opt) followed by arg words (and opt type and string)} 
    body [block!] "The body block of the function" 
    /local spc bdy word
    spc: make block! 1 + (2 * length? spec) 
    insert/only spc [throw] 
    bdy: make block! 5 + length? spec 
    insert bdy reduce [:do :make :function! spc body] 
    parse spec [any [
            set-word! | set word any-word! (
                insert tail bdy to word! :word 
                insert tail spc to get-word! :word 
                insert/only tail spc [any-type!]
            ) | skip
    throw-on-error [make function! spec bdy]