iterator

List of symbols:

iter::enumerate, iter::file-iter, iter::filter, iter::for, iter::iter, iter::iter-or-single, iter::iter?, iter::list-iter, iter::map, iter::mk-iter, iter::once-iter, iter::range, iter::reduce, iter::repeat-iter, iter::string-iter, iter::vec-iter, iter::vec-iter-pair, iter::vec-iter-rev

iter::enumerate

Usage: (iter::enumerate iter start [SCRATCH] [SCRATCH] idx [SCRATCH] [SCRATCH] iter)

Iterator that wraps an iterator and generates pairs of current index and value. Index is 0 based by default, takes an optional second parameter with the start index.

Example:

(import iter)
(let (test-iter (enumerate (vec-iter [:a :b :c])))
    (let ([i v] (test-iter)) (test::assert-equal 0 i) (test::assert-equal :a v))
    (let ([i v] (test-iter)) (test::assert-equal 1 i) (test::assert-equal :b v))
    (let ([i v] (test-iter)) (test::assert-equal 2 i) (test::assert-equal :c v))
    (test::assert-equal :*iter-empty* (test-iter)))
(let (test-iter (enumerate (vec-iter [:a :b :c]) 5))
    (let ([i v] (test-iter)) (test::assert-equal 5 i) (test::assert-equal :a v))
    (let ([i v] (test-iter)) (test::assert-equal 6 i) (test::assert-equal :b v))
    (let ([i v] (test-iter)) (test::assert-equal 7 i) (test::assert-equal :c v))
    (test::assert-equal :*iter-empty* (test-iter)))

iter::file-iter

Usage: (iter::file-iter f [SCRATCH] [SCRATCH] iter)

Iterator that wraps a file. Each call produces the next line (with trailing newline).

Example:

(import iter)
(with-temp-file (fn (file-name)
    (let (tst-file (fopen file-name :create :truncate))
        (defer (fclose tst-file))
        (fprn tst-file "line 1")
        (fprn tst-file "line 2")
        (fprn tst-file "line 3")
        (fpr tst-file "line 4"))
    (let (tst-file (fopen file-name), test-iter (file-iter tst-file))
        (defer (fclose tst-file))
        (test::assert-equal "line 1\n" (test-iter))
        (test::assert-equal "line 2\n" (test-iter))
        (test::assert-equal "line 3\n" (test-iter))
        (test::assert-equal "line 4" (test-iter))
        (test::assert-equal :*iter-empty* (test-iter)))))

iter::filter

Usage: (iter::filter iter lambda [SCRATCH] [SCRATCH] iter)

Returns a filter-iter around iter. Iterator that applies a lambda to each element to determine if is returned- is lazy.

Example:

(let (test-iter (iter::filter (iter::vec-iter [1 2 3]) (fn (x) (not (= x 2)))))
    (test::assert-equal 1 (test-iter))
    (test::assert-equal 3 (test-iter))
    (test::assert-equal :*iter-empty* (test-iter)))

iter::for

Usage: (iter::for bind in items body [SCRATCH] [SCRATCH] i-name actual-bind)

Loops over each element in an iterator. The bind parameter is bound to the current element of items and is accessible in body. Body is evaluated a number of times equal to the items in the iterator.

Example:

(import iter)
(let (i 0)
    (for x in (range 0 11) (set! i (+ 1 i)))
    (test::assert-equal 11 i))
(let (v [:a :b :c], iter (enumerate (vec-iter v)))
    (for [idx, val] in iter (test::assert-equal v.~idx val)))

iter::iter

Usage: (iter::iter thing)

Return thing as an iterator if possible (if it is an iterator just return thing).

Example:

(import iter)
(test::assert-true (iter? (iter '(1 2 3))))
(test::assert-true (iter? (iter [1 2 3])))
(test::assert-true (iter? (iter "abc")))
(test::assert-true (iter? (iter (iter '(1 2 3)))))

iter::iter-or-single

Usage: (iter::iter-or-single thing)

Return thing as an iterator if possible (if it is an iterator just return thing). If not possible then wrap thing in a once iter and return that.

Example:

(import iter)
(test::assert-true (iter? (iter-or-single '(1 2 3))))
(test::assert-true (iter? (iter-or-single [1 2 3])))
(test::assert-true (iter? (iter-or-single "abc")))
(test::assert-true (iter? (iter-or-single (iter '(1 2 3)))))
(test::assert-true (iter? (iter-or-single 1)))
(test::assert-true (iter? (iter-or-single \A)))

iter::iter?

Usage: (iter::iter? test)

Return true if thing is an iterator, false otherwise.

Example:

(import iter)
(test::assert-true (iter? (list-iter '(1 2 3))))
(test::assert-false (iter? '(1 2 3)))

iter::list-iter

Usage: (iter::list-iter l [SCRATCH] [SCRATCH] iter)

Iterator that wraps a list.

Example:

(import iter)
(let (test-list-iter (list-iter '(1 2 3)))
    (test::assert-equal 1 (test-list-iter))
    (test::assert-equal 2 (test-list-iter))
    (test::assert-equal 3 (test-list-iter))
    (test::assert-equal :*iter-empty* (test-list-iter))
    (set! test-list-iter (list-iter '()))
    (test::assert-equal :*iter-empty* (test-list-iter)))

iter::map

Usage: (iter::map i lambda [SCRATCH] [SCRATCH] iter)

Iterator that applies a lambda to each element of another iterator- is lazy.

Example:

(import iter)
(let (test-map-iter (map (list-iter '(1 2 3)) (fn (x) (* x 2))))
    (test::assert-equal 2 (test-map-iter))
    (test::assert-equal 4 (test-map-iter))
    (test::assert-equal 6 (test-map-iter))
    (test::assert-equal :*iter-empty* (test-map-iter)))

iter::mk-iter

Usage: (iter::mk-iter body)

Helper to create an iterator. Will make sure it has the correct property set.

Example:


iter::once-iter

Usage: (iter::once-iter i [SCRATCH] [SCRATCH] iter)

Iterator that wraps and returns a single object once.

Example:

(import iter)
(let (test-iter (once-iter 3))
    (test::assert-equal 3 (test-iter))
    (test::assert-equal :*iter-empty* (test-iter))
    (set! test-iter (once-iter "iter"))
    (test::assert-equal "iter" (test-iter))
    (test::assert-equal :*iter-empty* (test-iter)))

iter::range

Usage: (iter::range start end [SCRATCH] [SCRATCH] iter)

Iterator that generates numbers within a range.

Example:

(import iter)
(let (test-iter (range 3 6))
    (test::assert-equal 3 (test-iter))
    (test::assert-equal 4 (test-iter))
    (test::assert-equal 5 (test-iter))
    (test::assert-equal :*iter-empty* (test-iter)))

iter::reduce

Usage: (iter::reduce iter acc reducing-fn [SCRATCH] [SCRATCH] #SYM:367:2 #SYM:367:3 #SYM:367:3 [SCRATCH] val)

reduce is used to amalgamate an iterator and an initial value, according to the reducing function provided. The reducing-fcn should be a function of two arguments. In the first iteration of reduce, the init-val will be used as the first argument to the reducing-fcn and (iter) will be used as the second argument. For all subsequent iterations, The result from the previous application of the reducing-fcn will be used as the first argument to the reducing-fcn and the second argument will be the next item in the collection when the collection is empty reduce will return the amalgamated result.

Example:

(import iter)
(test::assert-true (= 15 (iter::reduce (iter::vec-iter [1 2 3 4 5]) 0 +)))
(test::assert-true (= 16 (iter::reduce (iter::vec-iter [1 2 3 4 5]) 1 +)))
(test::assert-true (= "one hoopy frood" (iter::reduce (iter::vec-iter ["one " "hoopy " "frood"]) "" str)))

iter::repeat-iter

Usage: (iter::repeat-iter i [SCRATCH] [SCRATCH] iter)

Iterator that wraps and returns a single object on each call.

Example:

(import iter)
(let (test-iter (repeat-iter 3))
    (test::assert-equal 3 (test-iter))
    (test::assert-equal 3 (test-iter))
    (test::assert-equal 3 (test-iter))
    (test::assert-equal 3 (test-iter))
    (test::assert-equal 3 (test-iter))
    (set! test-iter (repeat-iter "iter"))
    (test::assert-equal "iter" (test-iter))
    (test::assert-equal "iter" (test-iter))
    (test::assert-equal "iter" (test-iter))
    (test::assert-equal "iter" (test-iter))
    (test::assert-equal "iter" (test-iter)))

iter::string-iter

Usage: (iter::string-iter s [SCRATCH] [SCRATCH] idx slen [SCRATCH] [SCRATCH] iter)

Iterator that wraps a string. Each element is the next character.

Example:

(import iter)
(let (test-string-iter (string-iter "123"))
    (test::assert-equal \1 (test-string-iter))
    (test::assert-equal \2 (test-string-iter))
    (test::assert-equal \3 (test-string-iter))
    (test::assert-equal :*iter-empty* (test-string-iter)))

iter::vec-iter

Usage: (iter::vec-iter v [SCRATCH] [SCRATCH] idx vlen [SCRATCH] [SCRATCH] iter)

Iterator that wraps a vector.

Example:

(import iter)
(let (test-vec-iter (vec-iter [1 2 3]))
    (test::assert-equal 1 (test-vec-iter))
    (test::assert-equal 2 (test-vec-iter))
    (test::assert-equal 3 (test-vec-iter))
    (test::assert-equal :*iter-empty* (test-vec-iter))
    (set! test-vec-iter (vec-iter (vec)))
    (test::assert-equal :*iter-empty* (test-vec-iter)))

iter::vec-iter-pair

Usage: (iter::vec-iter-pair v [SCRATCH] [SCRATCH] fidx bidx [SCRATCH] [SCRATCH] iter iter)

Return a pair of iterators, one forward and one reverse for a vector. The two iterators will not overlap (i.e. the forward and reverse will never produce the same items).

Example:

(import iter)
(let ([fwd-iter, rev-iter] (vec-iter-pair [1 2 3]))
    (test::assert-equal 1 (fwd-iter))
    (test::assert-equal 3 (rev-iter))
    (test::assert-equal 2 (fwd-iter))
    (test::assert-equal :*iter-empty* (rev-iter))
    (test::assert-equal :*iter-empty* (fwd-iter)))
(let ([fwd-iter, rev-iter] (vec-iter-pair (vec)))
    (test::assert-equal :*iter-empty* (fwd-iter))
    (test::assert-equal :*iter-empty* (rev-iter)))

iter::vec-iter-rev

Usage: (iter::vec-iter-rev v [SCRATCH] [SCRATCH] idx [SCRATCH] [SCRATCH] iter)

Iterator produces a vector in reverse.

Example:

(import iter)
(let (test-vec-iter (vec-iter-rev [1 2 3]))
    (test::assert-equal 3 (test-vec-iter))
    (test::assert-equal 2 (test-vec-iter))
    (test::assert-equal 1 (test-vec-iter))
    (test::assert-equal :*iter-empty* (test-vec-iter))
    (set! test-vec-iter (vec-iter-rev (vec)))
    (test::assert-equal :*iter-empty* (test-vec-iter)))