Split

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

USAGE

   SPLIT series dlm /into

DESCRIPTION

Split a series into pieces; fixed or variable size, fixed number, or at delimiters

SPLIT is a function value.

ARGUMENTS

  • series -- The series to split (series!)
  • dlm -- Split size, delimiter(s), or rule(s). (block! integer! char! bitset! any-string!)

REFINEMENTS

  • /into -- If dlm is an integer, split into n pieces, rather than pieces of length n.

#SOURCE

split: make function! [  [
    {Split a series into pieces; fixed or variable size, fixed number, or at delimiters}
    series [series!] "The series to split"
    dlm [block! integer! char! bitset! any-string!] "Split size, delimiter(s), or rule(s)."
    /into {If dlm is an integer, split into n pieces, rather than pieces of length n.}
    /local size count mk1 mk2
][
    either all [block? dlm parse dlm [some integer!] ] [
        map-each len dlm [
            either positive? len [
                copy/part series series: skip series len
            ] [
                series: skip series negate len
                ()
            ]
        ]
    ] [
        size: dlm
        collect [
            parse/all series case [
                all [integer? size into] [
                    if size < 1 [cause-error 'Script 'invalid-arg size]
                    count: size - 1
                    size: round/down divide length? series size
                    [
                        count [copy series size skip (keep/only series)]
                        copy series to end (keep/only series)
                    ]
                ]
                integer? dlm [
                    if size < 1 [cause-error 'Script 'invalid-arg size]
                    [any [copy series 1 size skip (keep/only series)] ]
                ]
                'else [
                    [any [mk1: [to dlm mk2: dlm | to end mk2:] (keep copy/part mk1 mk2)] ]
                ]
            ]
        ]
    ]
] ]