[REBOL] [algorithm] Algorithm challenge: selecting a range from multiple blocks
From: gregg:pointillistic at: 20-Sep-2007 11:05
Hi Sunanda,
S> I coded my solution to it yesterday - and it's basic hack work. I
S> am sure there is a more elegant way - and probably several :-)
Basic hack work here too, to prime my pump this morning. We'll see if
something more elegant comes from my subconscious later.
Requires my COLLECT func, but can easily adapt to do without it.
Modifies the input.
Not tested for edge cases; just the examples given.
decr: func [
"Decrement a value"
word [word!]
/by "Change by this amount"
value
][
set word subtract get word any [value 1]
]
get-subset: func [
data [block!]
range [block!]
/local skip-count copy-count blk len
][
skip-count: range/1 - 1
copy-count: range/2 - range/1 + 1
collect obj [
while [not empty? data] [
blk: data/1/items
if skip-count > 0 [
len: length? blk
remove/part blk skip-count
decr/by 'skip-count len
]
if all [not empty? blk copy-count > 0] [
clear at blk copy-count + 1
decr/by 'copy-count length? blk
if not empty? blk [obj: data/1]
]
if copy-count <= 0 [break]
data: next data
]
]
]
-- Gregg