跳轉到內容

Rebol 程式設計/extract

來自華夏公益教科書,開放書籍,開放世界
EXTRACT series width /index pos /default value /into output 

從系列中以固定間隔提取值。

EXTRACT 是一個函式值。

  • series -- (型別:系列)
  • width -- 每個條目的大小(跳過)(型別:整數)
  • /index -- 從偏移位置提取
    • pos -- 位置 (型別:任何)
  • /default -- 使用預設值而不是無
    • value -- 要使用的值(如果是函式,每次都會被呼叫)(型別:任何)
  • /into -- 將其插入到緩衝區中 (返回插入後的位置)
    • output -- 緩衝區系列 (已修改) (型別:系列)

(特殊屬性)

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

原始碼

[編輯 | 編輯原始碼]
extract: func [
  {Extracts a value from a series at regular intervals.} 
  [catch] 
  series [series!] 
  width [integer!] "Size of each entry (the skip)" 
  /index "Extract from an offset position" 
  pos "The position" [number! logic! block!] 
  /default "Use a default value instead of none" 
  value {The value to use (will be called each time if a function)} 
  /into {Insert into a buffer instead (returns position after insert)} 
  output [series!] "The buffer series (modified)" 
  /local len val
][
  if zero? width [return any [output make series 0]] 
  len: either positive? width [
    divide length? series width
  ] [
    divide index? series negate width
  ] 
  unless index [pos: 1] 
  either block? pos [
    if empty? pos [return any [output make series 0]] 
    parse pos [some [number! | logic! | set pos skip (
          throw-error 'script 'expect-set reduce [[number! logic!] type? get/any 'pos]
        )]] 
    unless into [output: make series len * length? pos] 
    if all [not default any-string? output] [value: copy ""] 
    if binary? series [series: as-string series] 
    forskip series width [forall pos [
        if none? set/any 'val pick series pos/1 [set/any 'val value] 
        output: insert/only output get/any 'val
      ]]
  ] [
    unless into [output: make series len] 
    if all [not default any-string? output] [value: copy ""] 
    if binary? series [series: as-string series] 
    forskip series width [
      if none? set/any 'val pick series pos [set/any 'val value] 
      output: insert/only output get/any 'val
    ]
  ] 
  either into [output] [head output]
]
華夏公益教科書