Skip to content

Instantly share code, notes, and snippets.

@benton
Created October 27, 2016 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benton/66e5b7aca8a8522f44e16cc657d27fe7 to your computer and use it in GitHub Desktop.
Save benton/66e5b7aca8a8522f44e16cc657d27fe7 to your computer and use it in GitHub Desktop.
Create map from alternating series of keys and values
(defn make-pairs
"Creates a vector of pairs from the incoming sequence. Ignores extra elements."
([] []) ; return an empty vector if supplied no arguments
([seq] (make-pairs seq [])); return a vector of pairs taken from seq
([seq pairs] ; return a vector of pairs, starting with some existing pairs
(if (< (count seq) 2) pairs
(make-pairs (rest (rest seq))
(conj (vec pairs) [(first seq) (second seq)])))))
(defn map-pairs
"Creates a map from pairs of values from seq. Ignores extra elements."
[seq]
(into {} (make-pairs seq)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment