2018-03-16 03:27:44 +00:00
|
|
|
# An example implementation of functional, lazy
|
2018-03-18 13:13:21 +00:00
|
|
|
# sequences, as in clojure. The lazy seq is essentially
|
|
|
|
# A lazy linked list, where the next value is a function
|
|
|
|
# that must be called (realizing it), and the memoized.
|
2018-03-16 03:27:44 +00:00
|
|
|
# Use with (import "./path/to/this/file" :prefix "seq/")
|
|
|
|
|
2018-03-19 18:51:18 +00:00
|
|
|
(defmacro delay [& forms]
|
|
|
|
"Lazily evaluate a series of expressions. Returns a function that
|
|
|
|
returns the result of the last expression. Will only evaluate the
|
|
|
|
body once, and then memoizes the result."
|
|
|
|
(def $state (gensym))
|
|
|
|
(def $loaded (gensym))
|
|
|
|
(tuple 'do
|
|
|
|
(tuple 'var $state nil)
|
|
|
|
(tuple 'var $loaded nil)
|
|
|
|
(tuple 'fn (array)
|
|
|
|
(tuple 'if $loaded
|
|
|
|
$state
|
|
|
|
(tuple 'do
|
|
|
|
(tuple ':= $loaded true)
|
|
|
|
(tuple ':= $state (tuple-prepend forms 'do)))))))
|
2018-03-16 03:27:44 +00:00
|
|
|
|
|
|
|
# Use tuples instead of structs to save memory
|
|
|
|
(def HEAD :private 0)
|
|
|
|
(def TAIL :private 1)
|
|
|
|
|
|
|
|
(defn empty-seq
|
|
|
|
"The empty sequence."
|
|
|
|
[] nil)
|
|
|
|
|
2018-03-18 14:18:41 +00:00
|
|
|
(defmacro cons
|
2018-03-16 03:27:44 +00:00
|
|
|
"Create a new sequence by prepending a value to the original sequence."
|
|
|
|
[h t]
|
2018-03-18 13:18:53 +00:00
|
|
|
(def x (tuple h t))
|
|
|
|
(fn [] x))
|
2018-03-16 03:27:44 +00:00
|
|
|
|
|
|
|
(defn empty?
|
|
|
|
"Check if a sequence is empty."
|
|
|
|
[s]
|
|
|
|
(not (s)))
|
|
|
|
|
|
|
|
(defn head
|
|
|
|
"Get the next value of the sequence."
|
|
|
|
[s]
|
|
|
|
(get (s) HEAD))
|
|
|
|
|
|
|
|
(defn tail
|
|
|
|
"Get the rest of a sequence"
|
|
|
|
[s]
|
|
|
|
(get (s) TAIL))
|
|
|
|
|
2018-03-16 17:40:10 +00:00
|
|
|
(defn range2
|
2018-03-16 03:27:44 +00:00
|
|
|
"Return a sequence of integers [start, end)."
|
|
|
|
[start end]
|
|
|
|
(if (< start end)
|
2018-03-18 14:18:41 +00:00
|
|
|
(delay (tuple start (range2 (+ 1 start) end)))
|
2018-03-16 03:27:44 +00:00
|
|
|
empty-seq))
|
|
|
|
|
2018-03-16 17:40:10 +00:00
|
|
|
(defn range
|
|
|
|
"Return a sequence of integers [0, end)."
|
|
|
|
[end]
|
|
|
|
(range2 0 end))
|
|
|
|
|
2018-03-19 18:51:18 +00:00
|
|
|
(defn map
|
2018-03-16 03:27:44 +00:00
|
|
|
"Return a sequence that is the result of apply f to each value in s."
|
|
|
|
[f s]
|
2018-03-16 17:40:10 +00:00
|
|
|
(delay
|
|
|
|
(def x (s))
|
2018-03-19 18:51:18 +00:00
|
|
|
(if x (tuple (f (get x HEAD)) (map f (get x TAIL))))))
|
2018-03-16 03:27:44 +00:00
|
|
|
|
|
|
|
(defn realize
|
|
|
|
"Force evaluation of a lazy sequence."
|
|
|
|
[s]
|
|
|
|
(when (s) (realize (tail s))))
|
|
|
|
|
|
|
|
(defn realize-map [f s]
|
|
|
|
"Evaluate f on each member of the sequence. Forces evaluation."
|
|
|
|
(when (s) (f (head s)) (realize-map f (tail s))))
|
|
|
|
|
|
|
|
(defn drop
|
|
|
|
"Ignores the first n values of the sequence and returns the rest."
|
|
|
|
[n s]
|
2018-03-16 17:40:10 +00:00
|
|
|
(delay
|
|
|
|
(def x (s))
|
2018-03-18 14:18:41 +00:00
|
|
|
(if (and x (pos? n)) ((drop (- n 1) (get x TAIL))))))
|
2018-03-16 03:27:44 +00:00
|
|
|
|
|
|
|
(defn take
|
|
|
|
"Returns at most the first n values of s."
|
|
|
|
[n s]
|
2018-03-18 14:18:41 +00:00
|
|
|
(delay
|
|
|
|
(def x (s))
|
|
|
|
(if (and x (pos? n))
|
|
|
|
(tuple (get x HEAD) (take (- n 1) (get x TAIL))))))
|
2018-03-16 03:27:44 +00:00
|
|
|
|
2018-03-18 13:13:21 +00:00
|
|
|
(defn randseq
|
|
|
|
"Return a sequence of random numbers."
|
|
|
|
[]
|
|
|
|
(delay (tuple (random) (randseq))))
|
|
|
|
|
2018-03-16 17:40:10 +00:00
|
|
|
(defn take-while
|
|
|
|
"Returns a sequence of values until the predicate is false."
|
|
|
|
[pred s]
|
2018-03-16 19:45:24 +00:00
|
|
|
(delay
|
|
|
|
(def x (s))
|
|
|
|
(when x
|
|
|
|
(def thehead (get HEAD x))
|
|
|
|
(if thehead (tuple thehead (take-while pred (get TAIL x)))))))
|