Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Chain of handlers
;; raek's idea
(defn cool? [w]
true) ;; all words are cool
(def handlers
[[#(and (:small-word %)
(:first-word %)) #(.toUpperCase %)]
; you can even use functions as your predicate
[cool? #(.toLowerCase %)]])
(defn chain [handlers [word tags]]
(loop [h handlers]
(if (empty? h)
word
(let [[pred? action] (first h)]
(if (pred? tags)
(action word)
(recur (rest h)))))))
(chain handlers
["hello" {:small-word true
:first-word true}])
;; user=> "HELLO"
(map (partial chain handlers)
[["hello" {:small-word true
:first-word true}]
["wOrLd" {}]])
;; user=> ("HELLO" "world")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.