Skip to content

Instantly share code, notes, and snippets.

@cairesr
Last active September 4, 2016 21:44
Show Gist options
  • Save cairesr/50b33c6102820ebaee27bc5f00489489 to your computer and use it in GitHub Desktop.
Save cairesr/50b33c6102820ebaee27bc5f00489489 to your computer and use it in GitHub Desktop.
Clojure Seq Functions (based on Clojure for the Brave and True)

Maps with multiple collections

As long as the function used can receive the number of params you're passing on, you can pass multiple collections to map.

(map str ["Y" "Y" "Z"] ["y" "y" "z"]) ; => ("Yy" "Yy" "Zz")

You can pass the map a collection of functions.

(map #(% [1 2 3]) [count #(reduce + %)]) ; => (3 6)

You can use the map function to retrieve the values of a symbol of a map seq

(def ballads
    [{:name "Apocalypse Dreams"        :band "Tame Impala"}
     {:name "Lucidity"                 :band "Tame Impala"}
     {:name "I think I had a Headache" :band "QOSA"       }])
)

(map :name ballads) ; => ("Apocalypse Dreams" "Lucidity" "I think I had a Headache")

Reduce with maps

(def numeric-matcher
  #"^[\d]+$")

(defn reducing-the-map
  [ma-map]
  (reduce (fn [new-map [key val]]
            (assoc new-map key (if (re-find numeric-matcher (str val)) 
                                 (inc val) 
                                 val))) 
          {}
          ma-map))

(reducing-the-map {:go "horse" :deliver-days 3 :feasible? "naaaah" :pls-no-waterfall true}

; => {:go "horse", :deliver-days 4, :feasible? "naaaah", :pls-no-waterfall true}

take-while

Returns all the elements of a sequence while the condition is true

(def line-up
  [{:music "Beast Feast" :applauses true}
   {:music "Do you want more?" :applauses true}
   {:music "Hunt to food" :applauses false}
   {:music "Let's us leave the Earth" :applauses true}])
(take-while #(= (:applauses %) true) line-up)
; => ({:music "Beast Feast", :applauses true} {:music "Do you want more?", :applauses true})

drop-while

Don't return the elements of the seq until it reaches one with the given condition

(drop-while #(= (:applauses %) true) line-up)
;=> ({:music "Hunt to food", :applauses false} {:music "Let's us leave the Earth", :applauses true})

filter

Return all the elements selected by the given condition.

(filter #(= (:applauses %) true) line-up)
;=>  ({:music "Beast Feast", :applauses true} {:music "Do you want more?", :applauses true} {:music "Let's us leave the Earth", :applauses true})

In some cases, the difference between filter and take-while or drop-while is that filter traverses all the collection but take-while or drop-while reads the elements until the it reaches the given condition. So filter can be less efficient than the other two.

some

Returns true when the first element matches the given condition. Returns nil if there is no match.

(some #(= (:applauses %) true) line-up) ;=> true

sort-by

Sort by a function. It's sometimes called as key function

(sort-by #(:music %) line-up)

; => ({:music "Beast Feast", :applauses true} {:music "Do you want more?", :applauses true} {:music "Hunt to food", :applauses false} {:music "Let's us leave the Earth", :applauses true})

Lazy-seq

Gives back a sequence, but only evaluates the required elements. So

(take 10 (take 1000000 (repeat "na"))) ;=> ("na" "na" "na" "na" "na" "na" "na" "na" "na" "na")

will iterate 10 times only, because the outer take function only requires 10 elements of the lazy-seq. In this case this is possible because repeat function gives back a lazy-seq. There are some other functions that return lazy-seqs too, like map.

apply

Explodes a seq to be passed to a function that expects a rest parameter.

(apply max [1 20 3 4]) ;=> 20

partial

It takes any numbers of argument and applies them to an already defined function

(def daily-bands (partial conj ["black mountain" "'stone temple pilots"]))

(daily-bands) ;=> ["black mountain" "'stone temple pilots"]
(daily-bands "sepultura" "The Black Angels") ;=> ["black mountain" "'stone temple pilots" "sepultura" "The Black Angels"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment