Created
September 15, 2020 08:08
-
-
Save mping-exo/40981fda0b6ee32371de8ab12bd6e80c to your computer and use it in GitHub Desktop.
interceptors.clj
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
(defn fn-1arity [one-arg] | |
(inc one-arg)) | |
(defn fn-future [res] | |
(d/future | |
(Thread/sleep 1000) | |
res)) | |
(defn fn-2arity [a b] | |
(+ a b)) | |
(defn side-effect [a] | |
(if (> a 10) | |
(throw (ex-info (format "Value > 10: %s" a) {:a a})))) | |
;; we want the following | |
{:context {:a 1}} | |
;; call (fn-1arity 1), overwrite :b | |
{:context {:a 1 :b 2}} | |
;; call (fn-2arity 1 3), add :c | |
{:context {:a 1 :b 2} :result 3} | |
;; call (fn-future [ctx]) to prove it works with deferred returns as easily | |
{:context {:a 1 :b 2} :result 3} | |
;;; this is the glue code | |
@(ixm/execute {:context {:a 1}} | |
[;; 1-arity is fine for chaining & lensing | |
(-> fn-1arity | |
(ix/in [:context :a]) | |
(ix/out [:context :b])) | |
;; 2-arity is clumsy... | |
(-> (fn [{{:keys [a b]} :context}] (fn-2arity a b)) | |
(ix/out [:result])) | |
;; deferreds work fine and can be chained to the next ix | |
(-> fn-future) | |
(-> (fn [_] (println "deferred finished!")) | |
(ix/discard)) | |
;; side-effects is called "discard" | |
;; try running the ixm chain with {:a 5} | |
(-> side-effect | |
(ix/lens [:result]) | |
(ix/discard))]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment