Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 31-38 of 38 total

rotate function

    rotate: func [
        "Rotate values in a series."
        series [series!]
        /left   "Rotate left (the default)"
        /right  "Rotate right"
        /part range [number!] "Rotate this many positions"  ; TBD series! support?
        /local offset pad
    ][
        range: any [all [range  range // length? series] 1]
        if any [empty? series  zero? range] [return series]
        either right [
            offset: does [skip tail series negate range]
            pad: copy offset
            head insert head clear offset pad
        ][
            pad: copy/part series range
            append remove/part series range pad
        ]
    ]

dup, make-blank-value, and shift functions

    ; used in SHIFT below
	dup: func [value len [integer!] /local type] [
        type: either series? value [value] [either char? value [""] [[]]]
		head insert/only/dup make type len value len
	]

    ; used in SHIFT below
    make-blank-value: func [type] [
        any [
            attempt [make type 0]
            attempt [make type ""]
            attempt [make type []]
            attempt [make type none]
        ]
    ]

    shift: func [
        "Shift values in a series; length doesn't change."
        series [series!]
        /left   "Shift left (the default)"
        /right  "Shift right"
        /part range [number!] "Shift this many positions"  ; TBD series! support?
        /with fill "Fill vacated slots with this value"
        /local pad
    ][
        range: any [range 1]
        if any [empty? series  0 = range] [return series]
        pad: dup any [fill make-blank-value last series] range
        either right [
            head insert head clear skip tail series negate range pad
        ][
            append remove/part series range pad
        ]
    ]

ROR (rotate-right) function

    ROR: func ["Rotate Right" s [series!] n [integer!]][
        n: n // length? s
        append copy at s ((length? s) - (n - 1)) copy/part s (length? s) - n
    ]

ROL (rotate-left) function

    ROL: func ["Rotate Left" s [series!] n [integer!]][
        n: n // length? s
        append copy at s add n 1 copy/part s n
    ]

SHR (shift-right) function

    SHR: func [
        "Shift Right"
        s [series!]
        n [integer!]
        /with fill
    ][
        append
            array/initial
                min n (length? s)       ; we should really ASSERT n <= length? s
                either with [fill][none]
            copy/part s ((length? s) - n)
    ]

SHL (shift-left) function

    SHL: func [
        "Shift-Left"
        s [series!]
        n [integer!]
        /with fill
    ][
        append
            copy skip s n
            array/initial
                min n (length? s)       ; we should really ASSERT n <= length? s
                either with [fill][none]
    ]

ends-with function

    ends-with?: func [
        "Returns true if the series ends with the value; false otherwise"
        series [series!]
        value
        /local pos
    ] [
         either pos: find/last/tail series value [tail? pos] [false]
    ]

begins-with? function

    begins-with?: func [
        "Returns true if the series begins with the value; false otherwise"
        series [series!]
        value
    ] [
        found? find/match series value
    ]
« Newer Snippets
Older Snippets »
Showing 31-38 of 38 total