I had an idea for a higher order function today that I'm not sure how to write. I have some rare, lazy infinite sequences, and I want to create an abstraction that allows me to check if a given number is in any of these lazy sequences. To improve performance, I wanted to push the sparse sequence values ββinto the hash map (or set), dynamically increasing the number of values ββin the hash map whenever necessary. Automatic memoization is not the answer here due to the limited lazy sections.
The code is probably the easiest to understand, so here is what I still have. How to change the following code so that the predicate uses a closed hash file, but if necessary increases the size of the hash map and redefines itself to use the new hash file?
(defn make-lazy-predicate [coll] "Returns a predicate that returns true or false if a number is in coll. Coll must be an ordered, increasing lazy seq of numbers." (let [in-lazy-list? (fn [n coll top cache] (if (> top n) (not (nil? (cache n))) (recur n (next coll) (first coll) (conj cache (first coll)))] (fn [n] (in-lazy-list? n coll (first coll) (sorted-set))))) (def my-lazy-list (iterate
How to solve this problem without returning to the imperative style?
source share