Created
November 8, 2023 07:56
-
-
Save KingMob/52a65cb9357bc87baf792fc6fc81433c to your computer and use it in GitHub Desktop.
Example of exception-handling in Manifold with transducers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(in-ns 'user) | |
(require '[manifold.stream :as s]) | |
(defn flaky | |
[x] | |
(if (< (rand-int 10) 2) | |
(throw (Exception. "flaky")) | |
x)) | |
(defn reliable-add-sentinel | |
[x] | |
(try | |
(flaky x) | |
(catch Exception _ | |
:got-an-exception))) | |
(defn stream-ex-test | |
[xform] | |
(let [s (s/stream 10 xform) | |
results {:put-all (s/put-all! s (range 100)) | |
:consume-async (s/consume-async #(do (println %) true) s)}] | |
(s/close! s) | |
results)) | |
(stream-ex-test (clojure.core/map flaky)) | |
(stream-ex-test (clojure.core/map reliable-add-sentinel)) | |
;; drop bad vals entirely | |
(stream-ex-test (comp (clojure.core/map reliable-add-sentinel) | |
(remove #(= :got-an-exception %)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment