Skip to content

Commit

Permalink
[v4] Add scan utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Sep 24, 2024
1 parent f70bfa8 commit ef79c95
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/taoensso/carmine_v4.clj
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,51 @@
;; TODO Try publish error message?
))))))

;;;;

(defn reduce-scan
"For use with `scan`, `hscan`, `zscan`, etc.
Takes:
- (fn scan-fn [cursor]) -> next scan result
- (fn rf [acc scan-result]) -> next accumulator
TODO Example"
[init scan-fn rf]
(loop [cursor "0" acc init]
(let [[next-cursor in] (scan-fn cursor)]
(if (= next-cursor "0")
(rf acc in)
(let [result (rf acc in)]
(if (reduced? result)
@result
(recur next-cursor result)))))))

(comment
(reduce-scan []
(fn scan-fn [cursor] (wcar {} (scan cursor)))
(fn rf [acc in] (into acc in))))

(defn reduce-kv-scan
"Like `reduce-scan` but:
- `rf` is (fn [acc k v]), as in `reduce-kv`.
- `rf` will never be called with the same key twice
(i.e. automatically de-duplicates elements).
TODO Example"
[init scan-fn rf]
(let [keys-seen_ (volatile! (transient #{}))]
(reduce-scan init scan-fn
(fn wrapped-rf [acc kvs]
(enc/reduce-kvs
(fn [acc k v]
(if (contains? @keys-seen_ k)
acc
(do
(vswap! keys-seen_ conj! k)
(enc/convey-reduced (rf acc k v)))))
acc
kvs)))))

;;;; Scratch

;; TODO For command docstrings
Expand Down

0 comments on commit ef79c95

Please sign in to comment.