Rebol Programming/extract

From Wikibooks, open books for an open world
Jump to navigation Jump to search

USAGE:[edit | edit source]

EXTRACT series width /index pos /default value /into output 

DESCRIPTION:[edit | edit source]

Extracts a value from a series at regular intervals.

EXTRACT is a function value.

ARGUMENTS[edit | edit source]

  • series -- (Type: series)
  • width -- Size of each entry (the skip) (Type: integer)

REFINEMENTS[edit | edit source]

  • /index -- Extract from an offset position
    • pos -- The position (Type: any)
  • /default -- Use a default value instead of none
    • value -- The value to use (will be called each time if a function) (Type: any)
  • /into -- Insert into a buffer instead (returns position after insert)
    • output -- The buffer series (modified) (Type: series)

(SPECIAL ATTRIBUTES)[edit | edit source]

  • catch

SOURCE CODE[edit | edit source]

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]
]