Tacitus provides lazy indexable ranges with optional memoization. They support append in constant time and a single range requires close to zero memory allocation when not memoized. Appending ranges allocates memory for closures so appending many small ranges gradually requires more memory. Single ranges can be indexed in the time taken to evaluate the transformation applied to the range. Appending ranges builds more complex closures that need to be evaluated when indexing which gradually increases the time taken to index into a range.
Non memoized ranges are completely immutable. Memoized ranges use mutable hash tables to cache function evaluation.
Create a range with between
.
TACITUS> (between 0 100)
#S(RANGE
:SIZE 101
:TRANSFORMATION #<CLOSURE (LAMBDA (I) :IN BETWEEN) {10039FA89B}>
:MEMOIZATION NIL)
Convert a range to an array with to-array
.
TACITUS> (to-array (between 0 5))
#(0 1 2 3 4 5)
Transform a range with fmap
.
TACITUS> (to-array (fmap (lambda (n) (* n 3)) (between 4 8)))
#(12 15 18 21 24)
Append ranges with append-ranges
.
TACITUS> (to-array (append-ranges (between 7 9) (between 0 4)))
#(7 8 9 0 1 2 3 4)
Reduce ranges with reduce-range
.
TACITUS> (reduce-range #'+ (between 0 10))
55
Index into a range with index
.
TACITUS> (index (between 5 10) 0)
5
Ranges are space efficient.
TACITUS> (time (defparameter *big-range* (between 0 10000000)))
(time (defparameter *big-range* (between 0 10000000)))
Evaluation took:
0.000 seconds of real time
0.000009 seconds of total run time (0.000008 user, 0.000001 system)
100.00% CPU
12,699 processor cycles
0 bytes consed
*BIG-RANGE*
TACITUS> (time (defparameter *big-array* (make-array 10000000 :initial-element 0)))
Evaluation took:
0.064 seconds of real time
0.064315 seconds of total run time (0.039342 user, 0.024973 system)
[ Run times consist of 0.063 seconds GC time, and 0.002 seconds non-GC time. ]
100.00% CPU
115,095,924 processor cycles
80,000,016 bytes consed
*BIG-ARRAY*
Appending ranges is fast.
TACITUS> (time (append-ranges (between 0 10000000) (between 0 10000000)))
Evaluation took:
0.000 seconds of real time
0.000003 seconds of total run time (0.000002 user, 0.000001 system)
100.00% CPU
2,647 processor cycles
0 bytes consed
Henry Steere
Copyright (c) 2021 Henry Steere
Licensed under the MIT License.