Functional patterns reviews

#1

I have seen in forum we discuss about books etc on functional pattern etc.

I wanted to init a thread where we could discuss code snippets/review them and propose maybe other way to do in a functional mode, or maybe learn new functional pattern in clojure, which we didn’t know. :slight_smile:

So let me try first :smiley_cat:

In a recent side project i have written something like this

On the high-level i’m using quarzite library.
The 1st function schedule the jobs and i use the 2nd with map and doall.

;; ...

;; create rocket-chat msg job with only 1 trigger 
;; it can be same for all. ( maybe later extend this.)
(defn schedule-job-and-trigger [jobs-map]
    (let [s  (-> (qs/initialize) qs/start)
        job (create-rocket-msg-job jobs-map)
        trigger (create-rocket-msg-trigger)]
  (qs/schedule s job trigger)))

(defn schedule-all-jobs-and-triggers [jobs]
  (doall (map schedule-job-and-trigger jobs) ))

Is there any better alternative way for doing this? i’m missing something ?

1 Like
#2

Since you want to run schedule-job-and-trigger for its side-effects and not its return value, use run! instead of map (so you won’t need the doall):

(run! schedule-job-and-trigger jobs)

Also, since you have a “per-item” function, it’s considered an anti-pattern to also define a “collection” function over it: instead of defining schedule-all-jobs-and-triggers, just call run! directly. See https://stuartsierra.com/2015/08/10/clojure-donts-redundant-map

There is a lot of good advice in that category: https://stuartsierra.com/tag/dos-and-donts

3 Likes
#3

Nice, hadn’t ever noticed run! before.