Skip to content

Instantly share code, notes, and snippets.

@alandipert
Forked from paulosuzart/gist:489922
Created July 26, 2010 04:28
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 alandipert/490178 to your computer and use it in GitHub Desktop.
Save alandipert/490178 to your computer and use it in GitHub Desktop.
(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.)))
@paulosuzart
Copy link

Really nice! Will try :) Monads are not so clear yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment