跳转到内容

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
    )
]
华夏公益教科书