REBOL Programming/for

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

USAGE:[edit]

FOR 'word start end bump body 

DESCRIPTION:[edit]

Repeats a block over a range of values.

FOR is a function value.

ARGUMENTS[edit]

  • word -- Variable to hold current value (Type: word)
  • start -- Starting value (Type: number series money time date char)
  • end -- Ending value (Type: number series money time date char)
  • bump -- Amount to skip each time (Type: number money time char)
  • body -- Block to evaluate (Type: block)

(SPECIAL ATTRIBUTES)[edit]

  • catch
  • throw

SOURCE CODE[edit]

for: func [
    "Repeats a block over a range of values." 
    [catch throw] 
    'word [word!] "Variable to hold current value" 
    start [number! series! money! time! date! char!] "Starting value" 
    end [number! series! money! time! date! char!] "Ending value" 
    bump [number! money! time! char!] "Amount to skip each time" 
    body [block!] "Block to evaluate" 
    /local result do-body op
][
    if (type? start) <> (type? end) [
        throw make error! reduce ['script 'expect-arg 'for 'end type? start]
    ] 
    do-body: func reduce [[throw] word] body 
    op: :greater-or-equal? 
    either series? start [
        if not same? head start head end [
            throw make error! reduce ['script 'invalid-arg end]
        ] 
        if (negative? bump) [op: :lesser?] 
        while [op index? end index? start] [
            set/any 'result do-body start 
            start: skip start bump
        ] 
        if (negative? bump) [set/any 'result do-body start]
    ] [
        if (negative? bump) [op: :lesser-or-equal?] 
        while [op end start] [
            set/any 'result do-body start 
            start: start + bump
        ]
    ] 
    get/any 'result
]