Extract

angerangel edited this page Jan 3, 2013 · 1 revision
Clone this wiki locally

USAGE

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

DESCRIPTION

Extracts a value from a series at regular intervals.

EXTRACT is a function value.

ARGUMENTS

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

REFINEMENTS

  • /index -- Extract from an offset position
    • pos -- The position(s) (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 -- The buffer series (modified) (series!)

#SOURCE

extract: make function! [  [
    {Extracts a value from a series at regular intervals.}
    series [series!]
    width [integer!] "Size of each entry (the skip)"
    /index "Extract from an offset position"
    pos "The position(s)" [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 [
        unless parse pos [some [number! | logic!] ] [cause-error 'Script 'invalid-arg reduce [pos] ]
        unless output [output: make series len * length? pos]
        if all [not default any-string? output] [value: copy ""]
        forskip series width [forall pos [
                if none? set/any 'val pick series pos/1 [set/any 'val value]
                output: insert/only output :val
            ] ]
    ] [
        unless output [output: make series len]
        if all [not default any-string? output] [value: copy ""]
        forskip series width [
            if none? set/any 'val pick series pos [set/any 'val value]
            output: insert/only output :val
        ]
    ]
    either into [output] [head output]
] ]