This is a fairly underbaked show & tell. I'd hoped to have developed it more, but I'm almost out of time to work on Rebol projects before other things take precedence once again
. Also, I've had so much success working with some of the principles used here that I wanted to get some of it out now while I have a chance. So here is: Iterators, Part I
Not included in this article is my grids iterator. It's a little involved in the setup, but that's ok:
import r3:rgchris:iterate
size: 612x792
grid: iterate/new iterators/grids [
/page size /margins 68x67 /gutter 14x14
/columns 7 /rows 8
]
This dovetails nicely with my SVG module in populating the grid:
import r3:rgchris:svg
svg/encode svg/create size [
loop 52 [
iterate/next grid
circle #[fill: grey] grid/middle .5 * minimum grid/width grid/height
]
]
The resultant SVG is a letter-sized page with fifty-two well-aligned circles. I used this principle in an unofficial MLB schedule (along with the SVG module):
Also not included is the general iterative nature of most of the 'codecs' I've added. The idea of a Rebol-mode Deflate decoder might seem tortuously (
) slow. However, it has a couple of features not available to the built-in decoder: it doesn't need to copy its input and it doesn't need to get to the end; the first is feature is useful for Deflate-encoded content within Zip files or PDFs (or indeed Xara files). The decoder terminates when the Deflate stream terminates, thus aiding decoders/unpackers of formats that don't tell you how long the Deflate stream is (see again, PDF, Xara).
import r3:rgchris:deflate
decoder: flate/decoders/new #{4bcf57484cc9492d5248cd5548ccc90100}
decoder/window: 3
to string! flate/decoders/next decoder
=> "go "
The HTML decoder works along similar lines. Caveat, HTML can't be normalized without creating a full DOM-tree first—any iterative approach tied to parsing will be limited compared to one using the DOM. (neither iterator has been developed as yet to the Iterate standard)
import r3:rgchris:html
decoder: html/decoders/new "<b>Foo"
neaten/pairs collect-while [
html/decoders/next decoder
][
keep decoder/event
keep decoder/value
]
=> [
open "b"
text "Foo"
]
import r3:rgchris:html
decoder: dom/walk load-html "<b>Foo"
neaten/pairs collect-while [
decoder/next
][
keep decoder/event
keep any [
decoder/node/name
decoder/node/value
]
]
=> [
open _
open "html"
empty "head"
open "body"
open "b"
text "Foo"
close "b"
close "body"
close "html"
close _
]
The iterator/folders model in the linked article is another of my favourites.
It's good to get these scripts out even if I still have to document them and that the documentation will likely prompt alterations, and ARGH, time! I think there's enough here to warrant some lower-level integration (with PORTs, which is probably/possibly where they should be).
