跳转到内容

Rebol 编程/take

来自维基教科书,开放世界中的开放书籍
TAKE value /part length /last 

复制并从序列中删除。(修改)

TAKE 是一个函数值。

  • -- (类型:序列端口无)
  • /part -- 限制为给定长度或位置
    • 长度 -- (类型:数字序列端口对)
  • /last -- 从尾部获取

(特殊属性)

[编辑 | 编辑源代码]
  • 捕捉

源代码

[编辑 | 编辑源代码]
take: func [
    "Copies and removes from series. (Modifies)" 
    [catch] 
    value [series! port! none!] 
    /part "Limits to a given length or position" 
    length [number! series! port! pair!] 
    /last "Take it from the tail end"
][
    if value [
        either part [
            case [
                pair? length [
                    unless image? value [
                        throw-error 'script 'invalid-part length
                    ] 
                    last: none
                ] 
                any [series? length port? length] [
                    either same? head value head length [
                        length: subtract index? length index? value
                    ] [
                        throw-error 'script 'invalid-part reduce [length]
                    ]
                ]
            ] 
            if last [
                length: negate length 
                value: tail value
            ] 
            also copy/part value length remove/part value length
        ] [
            also pick either last [
                value: back tail value
            ] [value] 1 remove value
        ]
    ]
]
华夏公益教科书