Эквивалент цикла for в красном?

Я хочу использовать для http://www.rebol.com/docs/words/wfor.html для красного не работает.

Какой эквивалент?


person user310291    schedule 14.12.2017    source источник


Ответы (1)


Поскольку for в Rebol2 — это всего лишь мезонин, вы можете написать свой собственный for

for: func [
    "Repeats a block over a range of values." 
    [catch throw] 
    'word [word!] "Variable to hold current value" 
    start [number! series! time! date! char!] "Starting value" 
    end [number! series!  time! date! char!] "Ending value" 
    bump [number!  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
]

но вы также можете найти в сети несколько более мощных или более похожих на c-синтаксис версий, например. предложение для Rebol3

cfor: func [  ; Not this name
    "General loop based on an initial state, test, and per-loop change."
    init [block! object!] "Words & initial values as object spec (local)"
    test [block!] "Continue if condition is true"
    bump [block!] "Move to the next step in the loop"
    body [block!] "Block to evaluate each time"
    /local ret
] [
    if block? init [init: make object! init]
    test: bind/copy test init
    body: bind/copy body init
    bump: bind/copy bump init
    while test [set/any 'ret do body do bump get/any 'ret]
]

Red также предлагает возможность определить свой собственный макрос и скомпилировать его.

person sqlab    schedule 14.12.2017