Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(defn fn-1arity [one-arg]
(inc one-arg))
(defn fn-future [res]
(Thread/sleep 1000)
(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!"))
;; side-effects is called "discard"
;; try running the ixm chain with {:a 5}
(-> side-effect
(ix/lens [:result])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment