Skip to content

Instantly share code, notes, and snippets.

@ray1729
Created April 5, 2011 08:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ray1729/903268 to your computer and use it in GitHub Desktop.
Save ray1729/903268 to your computer and use it in GitHub Desktop.
(defn parse-flags
"Parse a list of flags. Each flag name must be a keyword, and the
value must not be a keyword. If the value is omitted, true is
assumed. Returns a map.
e.g. (parse-flags [:i :j 1 :k \"two\"])
=> {:i true, :j 1, :k \"two\"}"
[flags]
(loop [flags (seq flags) accum {}]
(if flags
(let [k (first flags)]
(if (and (next flags) (not (keyword? (second flags))))
(recur (nnext flags) (assoc accum k (second flags)))
(recur (next flags) (assoc accum k true))))
accum)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment