-
-
Save alandipert/490178 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
(ns org.jtornadoweb.cljhttputils | |
(:use [clojure.contrib.monads :only (domonad maybe-m)] | |
[clojure.contrib.str-utils2 :only (split)]) | |
(:gen-class | |
:name org.jtornadoweb.CljHttpUtils | |
:methods [[parseQueryString [String] java.util.HashMap]] | |
:main false)) | |
(defn- parse-qs | |
[uri] | |
;; Arguable use of the maybe monad. | |
(domonad maybe-m | |
[query-string (last (re-find #"\?(.*)$" uri)) | |
pairs (split query-string #"&") | |
params (mapcat #(split % #"=") pairs)] | |
(when (even? (count params)) | |
(apply hash-map params))) | |
(defn #^:static -parseQueryString | |
[uri] | |
(if-let [params (parse-qs uri)] | |
(java.util.HashMap. params) | |
(java.util.HashMap.))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really nice! Will try :) Monads are not so clear yet.