Skip to content

Instantly share code, notes, and snippets.

@sw-samuraj
sw-samuraj / blog-ring-middleware-threading.clj
Last active May 1, 2017 11:15
An example of Ring middleware threading (for a blog post).
(def app
(-> handler
(wrap-keyword-params)
(wrap-params)))
@sw-samuraj
sw-samuraj / blog-ring-wrap-params.clj
Created May 1, 2017 06:43
An example of Ring middlewares wrap-params and wrap-keyword-params (for a blog post).
(ns blog-ring.middleware
(:require [ring.middleware.params :refer [wrap-params]]
[ring.middleware.keyword-params :refer [wrap-keyword-params]]))
((wrap-params identity)
{:query-string "clojure=yes&lisp=maybe"})
;; -> {:query-string "clojure=yes&lisp=maybe",
;; :form-params {},
;; :params {"clojure" "yes", "lisp" "maybe"},
;; :query-params {"clojure" "yes", "lisp" "maybe"}}
@sw-samuraj
sw-samuraj / blog-ring-request.clj
Created April 29, 2017 20:29
An example of Ring request (for a blog post).
(defn handler [request]
{:status 200
:headers {"Content-Type" "text/html"}
:body (str "<h1>One Ring rules them all!</h1>"
"<ul><li>Request from IP: "
(:remote-addr request)
"</li><li>Request method: "
(:request-method request)
"</li><li>Headers: "
(select-keys
@sw-samuraj
sw-samuraj / blog-ring-response.clj
Created April 29, 2017 16:41
An example of Ring responses (for a blog post).
(ns blog-ring.response
(:require [ring.util.response :as res]))
(def simple-res (res/response "Hello, world!"))
simple-res
;; -> {:status 200,
;; :headers {},
;; :body "Hello, world!"}
@sw-samuraj
sw-samuraj / blog-ring-lein.clj
Created April 11, 2017 21:08
An example of the Leiningen project for a simple Ring handler (for a blog post). Raw
(defproject blog-ring "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.8.0"]
[ring "1.6.0-RC2"]]
:main blog-ring.core)
@sw-samuraj
sw-samuraj / blog-ring-handler-plain.clj
Last active May 1, 2017 19:25
An example of plain Ring handler (for a blog post).
(ns blog-ring.core
(:require [ring.adapter.jetty :as jetty]))
(defn handler [request]
{:status 200
:headers {"Content-Type" "text/html"}
:body "<h1>Hello, world!</h1>"})
(defn -main []
(jetty/run-jetty handler
@sw-samuraj
sw-samuraj / blog-currying-2.clj
Created April 4, 2017 09:14
An example of currying in Clojure with more parameters at once, for a blog post.
(defn spicy [first second third]
(println "I like food with" first "," second "and" third "."))
(def curry-and-chilli
(partial spicy "curry" "curry" "chilli"))
(curry-and-chilli)
;; -> I like food with curry , curry and chilli .
@sw-samuraj
sw-samuraj / blog-currying.clj
Last active April 4, 2017 09:08
An example of currying on Clojure for a blog post.
(defn spicy [first second third]
(println "I like food with" first "," second "and" third "."))
(def curry (partial spicy "curry"))
(def more-curry (partial curry "curry"))
(def curry-and-chilli (partial more-curry "chilli"))
(curry-and-chilli)
@sw-samuraj
sw-samuraj / blogCurrying.groovy
Created April 4, 2017 08:54
An example of currying in Groovy for a blog post.
def spicy = { first, second, third ->
println "I like food with $first, $second and $third."
}
def curry = spicy.curry("curry")
def moreCurry = curry.curry("curry")
def curryAndChilli = moreCurry.curry("chilli")
curryAndChilli.call()
// prints:
@sw-samuraj
sw-samuraj / blog-concurrency-var-ns.clj
Last active March 25, 2017 17:47
An example of Var referring from the other namespace.
;; the initial namespace in REPL is 'user'
(def my-var "var in the user namespace")
(ns new-namespace)
my-var
;; -> CompilerException java.lang.RuntimeException:
;; Unable to resolve symbol: my-var in this context