跳轉到內容

Rebol 程式設計/map-each

來自華夏公益教科書,開放的書籍,開放的世界
MAP-EACH 'word data body /into output 

針對系列中的每個值(s)評估塊並將其作為塊返回。

MAP-EACH 是一個函式值。

  • word -- 每次設定的單詞或單詞塊(本地)(型別:單詞 塊)
  • data -- 要遍歷的系列(型別:塊)
  • body -- 每次評估的塊(型別:塊)
  • /into -- 收集到給定的系列中,而不是新的塊中
    • output -- 要輸出到的系列(型別:任何塊 任何字串)

(特殊屬性)

[編輯 | 編輯原始碼]
  • 丟擲
  • 捕獲

原始碼

[編輯 | 編輯原始碼]
map-each: func [
    {Evaluates a block for each value(s) in a series and returns them as a block.} 
    [throw catch] 
    'word [word! block!] "Word or block of words to set each time (local)" 
    data [block!] "The series to traverse" 
    body [block!] "Block to evaluate each time" 
    /into {Collect into a given series, rather than a new block} 
    output [any-block! any-string!] "The series to output to" 
    /local init len x
][
    if empty? data [return any [output make block! 0]] 
    word: either block? word [
        if empty? word [throw make error! [script invalid-arg []]] 
        copy/deep word
    ] [reduce [word]] 
    word: use word reduce [word] 
    body: bind/copy body first word 
    init: none 
    parse word [any [word! | x: set-word! (
                unless init [init: make block! 4] 
                insert insert insert tail init first x [at data] index? x 
                remove x
            ) :x | x: skip (
                throw make error! reduce ['script 'expect-set [word! set-word!] type? first x]
            )]] 
    len: length? word 
    unless into [output: make block! divide length? data max 1 len] 
    until [
        set word data do init 
        unless unset? set/any 'x do body [output: insert/only output :x] 
        tail? data: skip data len
    ] 
    also either into [output] [head output] (
        set [word data body output init x] none
    )
]
華夏公益教科書