Last active
July 5, 2022 13:27
-
-
Save sw1nn/4c782724084b164f9f23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Based on https://clojuredocs.org/clojure.core/condp#example-542692cbc026201cdc326bea | |
(defn parse-arg [s] | |
(condp (comp next re-matches) s | |
#"([+-])(\w+)" :>> (fn [[f k]] [(keyword k) (= f "+")]) | |
#"(\w+)=(\w+)" :>> (fn [[k v]] [(keyword k) v]) | |
(throw (IllegalArgumentException. s)))) | |
(parse-arg "+foo") ;=> [:foo true] | |
(parse-arg "-foo") ;=> [:foo false] | |
(parse-arg "foo=bar") ;=> [:foo "bar"] | |
(->> ["+foo" "-bar" "baz=quuz"] | |
(map parse-arg) | |
(into {})) ;=> {:foo true, :bar false, :baz "quuz"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment