Tuesday, January 13, 2015

Anonymous functions, PAFs and map in clojure

Anonymous functions, PAFs and map in clojure

 
;; Two ways to write a partially applied functions in clojure (PAF)((fn [x] (str "Hi, " x)) "there.")
((partial str "Hi, ") "there.")
;; Define these PAFs(def addhi1 (fn [x] (str "Hi, " x)))
(def addhi2 (partial str "Hi, "))
;; Map these two PAFs to a list, results are the same(map addhi1 ["Newon", "Einstein"])
(map addhi2 ["Newon", "Einstein"])
;; Passing multiple args to a vector(defn addhi-map1 [& xs] (map addhi1 xs))
(defn addhi-map2 [& xs] (map addhi2 xs))
(addhi-map1 "Newton" "Einstein")
(addhi-map2 "Newton" "Einstein")

0 comments: