Clojure Programming/Examples/API Examples/Sequence Building

From Wikibooks, open books for an open world
Jump to: navigation, search

conj[edit]

user=> (conj [1 2 3] 4)
[1 2 3 4]

user=> (conj '(:a :b :c) \d)
(\d :a :b :c)

concat[edit]

(defn poly-expand
  poly-expand [points]
  (loop [aa (first points) remaining (rest points) built (empty points)]
    (if (empty? remaining)
      (concat built [aa (first points)])
      (recur (first remaining) (rest remaining)
                     (concat built [aa (first remaining)])))))
(poly-expand '[a b c d])
-> [a b b c c d d a]

merge[edit]

Combine two maps into a more massive map

(merge {:a 1} {:b 2}) => {:a 1, :b 2}) (merge-with + {:a 1} {:a 2, :b 3}) => {:a 3 :b 3}