Repeatedly apply fun to data until (equal old-data new-data)
returns true. If max iterations occur, it will throw an
exception. Set max to nil for unlimited iterations.
(defn fixed-point
"Repeatedly apply fun to data until (equal old-data new-data)
returns true. If max iterations occur, it will throw an
exception. Set max to nil for unlimited iterations."
[data fun max equal]
(let [step (fn step [data idx]
(when (and idx (= 0 idx))
(throw (Exception. "Fixed point overflow")))
(let [new-data (fun data)]
(if (equal data new-data)
new-data
(recur new-data (and idx (dec idx))))))]
(step data max)))
Comments top
No comments for fixed-point. Log in to add a comment.