Skip to content

Instantly share code, notes, and snippets.

@janherich
Last active May 29, 2018 22:40
Show Gist options
  • Save janherich/918b605685d5dcb3e60548ba45fbf8a2 to your computer and use it in GitHub Desktop.
Save janherich/918b605685d5dcb3e60548ba45fbf8a2 to your computer and use it in GitHub Desktop.
Current situation
;; All our model functions adhere to simple interface, taking any number of arguments,
;; but always the `:cofx` map as an last argument and producing map of effects (or nothing)
;; as result:
(defn effects-fn [arg1 arg2 {:keys [db] :as cofx}]
{:db (update db :some-key arg1)
:http-call ...})
;; We have both `merge-fx` macro and `merge-effect` functions at our disposal
;; Whenever we do static merging of effects, we use the former:
(defn final-effects-fn [cofx]
(merge-fx cofx
(effects-fn "a" "b")
(effects-fn1)))
;; But when we wan to produce dynamic effects (we don't know at the compile time, how
;; many times we will need to call some effects producing function), we have to use the
;; latter, `merge-effects`
(defn final-effects-fn [cofx]
(merge-effects cofx
(fn [sequence-value cofx]
(effects-fn "a" sequence-value cofx))
some-dynamic-sequence-of-values))
;; The above is quite confusing for anybody not familiar with the codebase + some code
;; (which takes care of the proper cofx passing/effects merging) is duplicated in `merge-fx`
;; and `merge-effects`. Additionaly, because `merge-fx` is macro, below won't work:
(defn final-effects-fn [cofx]
(merge-fx cofx
(when some-condition
(effects-fn "a" "b"))
(effects-fn1)))
;; If conditional effects merging is desired, it must be always included in the inner functions
;; (so the when condition has to be in the `effects-fn` in the above case), which makes it harder
;; to reuse such functions and readibility/code clarity suffers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment