This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-03-20
Channels
- # admin-announcements (1)
- # announcements (9)
- # aws (11)
- # babashka (33)
- # beginners (125)
- # calva (20)
- # cider (18)
- # clj-kondo (7)
- # cljs-dev (73)
- # clojure (72)
- # clojure-europe (18)
- # clojure-italy (13)
- # clojure-nl (13)
- # clojure-uk (9)
- # clojurescript (22)
- # core-async (7)
- # cursive (1)
- # data-science (25)
- # datomic (22)
- # duct (32)
- # emacs (13)
- # graalvm (5)
- # hoplon (16)
- # juxt (6)
- # kaocha (8)
- # leiningen (3)
- # malli (11)
- # meander (12)
- # off-topic (18)
- # pathom (109)
- # pedestal (5)
- # rdf (10)
- # reagent (1)
- # reitit (12)
- # shadow-cljs (27)
- # spacemacs (5)
- # sql (9)
- # tools-deps (7)
I have a list of functions that take callbacks, and I want to exec them in order. e.g.
[proc1 proc2 proc3 ,,, procN]
and I want to exec:
(proc1 (fn [] (proc2 (fn [] (proc3 ,,,)))))
Recursive macro is probably the way to go.
Why the fn
s?
Why not just (proc1 (proc2 (proc3 ,,,)))
?
Oh, wait, nvm.
I'd just go with a macro.
so you write:
(def pipeline
(->
(task1)
(t/join (task2))
(t/join (task3))
,,,
(t/join (taskN))))
and then sometime later:
(t/run! pipeline #(prn "done"))
right now, each t/join
creates a new Task that calls run!
when the previous task succeeds, building the callback chain on instantiation
I was wondering if it would be better to store each task in a queue and wire up the callback chain on run!
, but that seems harder
I wrote a basic macro that does what you want, I think. I'm not sure about the end-case.
(defmacro callbacks
[proclist]
(if (= (count proclist) 1)
(first proclist)
`(~(first proclist) (fn [] (callbacks ~(rest proclist))))))
(clojure.walk/macroexpand-all '(callbacks [:proc1 :proc2 :proc3 :proc4]))
;; => (:proc1 (fn* ([] (:proc2 (fn* ([] (:proc3 (fn* ([] :proc4)))))))))
There's probably a more efficient way using a loop
, but that works.
Just curious lilactown (granted, I don't know if this is the most elegant) you are referring to something like this right
(defn run-callbacks [callbacks]
(let [ run-callbacks-fn
(->> callbacks
reverse
(reduce
(fn [inner-procs proc]
(fn [next] (proc (fn [] (inner-procs next)))))))]
(run-callbacks-fn (fn [] ))))
(run-callbacks [(fn [callback] (println "One") (callback))
(fn [callback] (println "Two") (callback))
(fn [callback] (println "Three") (callback))])
;; user>
;; One
;; Two
;; Three