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 11-20 of 38 total

add-escapes - escape chars in a string

    add-escapes: func [
        "Insert the esc-with value before each instance of val, unless it's part of a list of things to ignore."
        series   [string!]
        val      [char!] "the char to escape"
        esc-with [char!] "the char to escape it with"
        /ignore special [block!] "block of strings to ignore"
        /local rule=
    ][
        rule=: copy/deep [
            any [
                ; ignore rule will be added here if needed
                mark: val (insert mark esc-with) skip
                | skip
            ]
        ]
        ; If they have things they want us to ignore, add a rule at the
        ; beginning of the ANY rule, and a pipe symbol after it.
        if special [
            ; rule=/2 is the ANY block
            ; pipe 1 is DELIMIT param
            ; pipe 2 is new rule 'OR separator
            insert rule=/2 reduce [delimit copy special '| '|]
        ]
        parse/all series rule=
        series
    ]

remove-all - remove all values matching the value arg from the series

remove-all: func [series value] [remove-each val series [val = value]]

one-item? - simple wrapper to aid readability

one-item?: func [series] [1 = length? series]

select-next - like select+skip, gets the value following the value following the key

    select-next: func [
    	"Like select, but gets the value following the value following the key"
    	series [series!]
    	key [any-type!]
    	/local pos
    ][
    	if pos: find series key [
    		first skip pos 2
    	]
    ]

parse-replace - parse-based REPLACE function

    parse-replace: func [
        {Replaces the search value with the replace value within the target series.}
        target [series!] "Series that is being modified."
        search "Value to be replaced."
        replace "Value to replace with."
        /all "Replace all occurrences."
        /case "Case-sensitive replacement."
        /local len skip-len rule text
    ][
        len: length? form search
        skip-len: length? form replace
        rule: copy [
            [to search mark: (change/part mark replace len) skip-len skip]
            to end
        ]
        if all [insert rule 'any]
        either case [parse/all/case target rule] [parse/all target rule]
        target
    ]

spec-block-to-named-block

spec-block-to-named-block: func [
    "Change all set-words in a spec block to regular words."
    input [block!]
][
    input: copy input
    parse input [
        some [mark: set-word! any-type! (change mark to word! first mark)]
    ]
    input
]

change-all - change each value in a series by applying a function to them

change-all: func [
    "Change each value in the series by applying a function to it"
    series  [series!]
    fn      [function!] "Function that takes one arg"
][
    forall series [change series fn first series]
]

priority-queue functions

REBOL [
    Title: "Cheasy (Cheap-n-Easy) Priority Queue"
]

pq-insert: func [
    list [any-block!]   "The queue"
    item
    priority [integer!]
][
    sort/skip/reverse append list reduce [priority item] 2
]

pq-remove: func [
    "Remove an item from the priority queue"
    list [any-block!]   "The queue"
    /index  "Remove a specific item"
        idx [integer!] "The specific item to remove"
][
    remove/part either index [at list (idx * 2 - 1)][head list] 2
]

pq-first: func [
    list [any-block!]   "The queue"
][
    ; skip over the priority value and return the actual value
    ; that was inserted in the queue.
    first next head list
]

priority-queue: make object! [
    data: copy []

    insert: func [item priority] [pq-insert data item priority]

    remove: func [/index idx] [
        either index [pq-remove/index data idx][pq-remove data]
    ]

    first: does [pq-first data]
]



pq: copy []
print pq-insert pq "A" 1
print pq-insert pq "B" 10
print pq-insert pq "C" 100
print pq-insert pq "D" 1000
print pq-remove/index pq 3
print pq-remove pq
print mold pq
print pq-insert pq "CC" 100
print pq-insert pq "CCC" 100
print pq-insert pq "D" 1000
print pq-insert pq "CCCC" 100
print pq-insert pq "CCCCC" 100
print pq-remove pq
print pq-insert pq "CCCCCC" 100
print pq-insert pq "CCCCCCC" 100

print ""

pq: make priority-queue []
print pq/insert"A" 1
print pq/insert"B" 10
print pq/insert"C" 100
print pq/insert"D" 1000
print pq/remove/index 3
print pq/remove ;pq
print mold pq/data
print pq/insert"CC" 100
print pq/insert"CCC" 100
print pq/insert"D" 1000
print pq/insert"CCCC" 100
print pq/insert"CCCCC" 100
print pq/remove ;pq/data
print pq/insert"CCCCCC" 100
print pq/insert"CCCCCCC" 100


halt

insert-MRU MRU list function

insert-MRU: func [
    "Insert value in series; removing first existing instance."
    series [series!] 
    value 
    /limit size [integer!] "Limit the series to the given size by removing the last item."
][
    remove find/only series value
    insert/only series value
    if all [size  size < length? series] [remove back tail series]
    series
]

slice function

slice: func [series start len] [copy/part at series start len]
« Newer Snippets
Older Snippets »
Showing 11-20 of 38 total