Up: Eager comprehensions [Contents][Index]
In all of the examples below, the user must include the module ‘(data rope)’.
(rope-ec (:list s '("hello" " " "world")) s)
⇒ "hello world"
Example 2.11: Creating a rope from a list of strings.
(define r (rope "hello " "world"))
(list-ec (:rope s r 3 9) s)
⇒ ("lo " "wor")
(list-ec (:rope s r 8 2) s)
⇒ ("wor" "lo ")
Example 2.12: Eagerly comprehending over a portion of a rope, forward or backward.
(rope->cons
(parameterize ((*rope-maximum-leaf-size* 5))
(rope-ec (:list s '("foo" "bar" "baz"))
(string-append s (string-upcase s)))))
⇒ ((("fooFO" . "O") . "barBA") ("R" . "bazBA") . "Z")
Example 2.13: Creating a rope from mapped contents of a list.
Note that the above algorithm prefers fragmented ropes to tightly defragmented ones: the creation of the latter require more memory and time (potentially shifting the entire rope).
(rope-ec (:do ((s (thunk)))
(not (string-null? s))
((thunk)))
s)
Example 2.14: Creating a rope from a generating thunk.
(use-modules (ice-9 textual-ports))
;; We use a single buffer for the reading, to avoid reallocating
;; unnecessary buffers.
(let ((bufsiz 4096))
(call-with-input-file "example.txt"
(lambda (port)
(rope-ec (:let s (make-string bufsiz))
(:port x port
(lambda (p)
(let ((n (get-string-n! p s 0
(string-length s))))
(if (eof-object? n) n
(substring/shared s 0 n)))))
x))))
Example 2.15: Creating a rope from a file.
(define r (string->random-rope "seiks eulB"))
(string-ec (:rope s r (1- (rope-length' r)) -1)
(:range i (1- (string-length s)) -1 -1)
(string-ref s i))
⇒ "Blue skies"
Example 2.16: Processing the characters of a rope in reverse.
Note in the above example that the :rope generator accepts start
and end indices such that start > end, and then it traverses the rope
in reverse.
(define r1 (cons->rope '(("foo" . "bar") . "baz")))
(define r2 (string->random-rope "abcdef"))
(rope->string
(rope-ec (:list r (list r1 ": " r2))
(:rope s r)
s))
⇒ "foobarbaz: abcdef"
Example 2.17: Processing multiple ropes together.
Up: Eager comprehensions [Contents][Index]