跳轉到內容

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
        ]
    ]
]
華夏公益教科書