implement early termination using reduced, re-implement select-any/select-first in terms of it

This commit is contained in:
nathanmarz 2017-02-11 21:26:30 -05:00
parent f521409482
commit 80b3857b08
3 changed files with 24 additions and 24 deletions

View file

@ -1066,13 +1066,14 @@
(late-bound-richnav [late1 (late-path path1)
late2 (late-path path2)]
(select* [this vals structure next-fn]
;; on a select-any would be better to avoid doing late2 if late1 selects
;; something
(let [res1 (i/exec-select* late1 vals structure next-fn)
res2 (i/exec-select* late2 vals structure next-fn)]
(if (identical? NONE res2)
(let [res1 (i/exec-select* late1 vals structure next-fn)]
(if (reduced? res1)
res1
res2)))
(let [res2 (i/exec-select* late2 vals structure next-fn)]
(if (identical? NONE res1)
res2
res1
)))))
(transform* [this vals structure next-fn]
(let [s1 (i/exec-transform* late1 vals structure next-fn)]
(i/exec-transform* late2 vals s1 next-fn)))))

View file

@ -306,10 +306,14 @@
(compiled-traverse*
apath
(fn [elem]
(let [curr (get-cell cell)]
(set-cell! cell (afn curr elem))))
(let [curr (get-cell cell)
newv (afn curr elem)]
(set-cell! cell newv)
newv ; to support reduced handling during traverse
))
structure)
(get-cell cell)))))
(unreduced (get-cell cell))
))))
(defn compiled-select* [path structure]
@ -350,24 +354,19 @@
ret)))
(defn compiled-select-first* [path structure]
(let [res (mutable-cell NONE)
result-fn (fn [structure]
(let [curr (get-cell res)]
(if (identical? curr NONE)
(set-cell! res structure))))]
(compiled-traverse* path result-fn structure)
(let [ret (get-cell res)]
(if (identical? ret NONE)
nil
ret))))
(defn compiled-select-any* [path structure]
(compiled-traverse* path identity structure))
(unreduced (compiled-traverse* path reduced structure)))
(defn compiled-select-first* [path structure]
(let [ret (compiled-select-any* path structure)]
(if (identical? ret NONE)
nil
ret
)))
(defn compiled-selected-any?* [path structure]
(not= NONE (compiled-select-any* path structure)))
(not (identical? NONE (compiled-select-any* path structure))))
(defn terminal* [afn vals structure]
(if (identical? vals [])

View file

@ -6,7 +6,7 @@
(let [ret# (do ~@body)]
(if (identical? ret# ~backup-res)
curr#
ret#)))
(if (reduced? ret#) (reduced ret#) ret#))))
~backup-res
~aseq))