5. Appleholics
% brew install leiningen
Non Appleholics
Find Leiningen for Clojure on
https://github.com/technomancy/leiningen
% lein self-install
6. S-Expressions
Clojure:
(defn fn [arg1 & more] arg1)
defn is a macro used here with 3 parameters:
function name, arguments, and body.
Returns the function binding.
Clojure:
(println (fn value1 value2))
Java:
System.out.println(fn(value1, value2));
7. S-Expressions
Clojure:
(defn fn [arg1 & more] arg1)
defn is a macro used here with 3 parameters:
function name, arguments, and body.
Returns the function binding.
Clojure:
(println (fn value1 value2))
Java:
System.out.println(fn(value1, value2));
8. ring framework
(ns twitturl.core
(:require [com.twinql.clojure.http :as http])
(:use [ring.middleware.params])
(:use [hiccup.core]))
Import dependencies
(defn handler [] nil) ;todo create handler
(def app (-> handler wrap-params))
-> operator used apply multiple
Entry point functions to handler
9. De-structuring
(defn format-tweets [json] nil) ; todo
(defn handler [{{q "q"} :params}]
{ :content-type "text/html"
:body (html [:body
(-> (search q) format-tweets)])})
same as: Only need q
(defn handler [request]
(let [params (request :params) Local variable assignments
q (params "q")] request and params
{:content-type "text/html" unnecessary.
:body (html [:body
(-> (search q) format-tweets)])}))
10. The Search
(def url "http://search.twitter.com/search.json")
define readonly variable named url
(defn search [query]
(http/get url :query {:q query} :as :json))
returns result of com.twinql.clojure.http.get
using parameter list
1) url
2) :query
3) {:q query }
4) :as
5) :json
11. Enter the repl
% lein deps
Copying 22 files to /Users/kurtharriger/code/twitturl/lib
Copying 17 files to /Users/kurtharriger/code/twitturl/lib/dev
% lein repl
REPL started; server listening on localhost:24319.
user=> (use ‘twitturl.core)
nil
user=> (search “clojure”)
{:code 200,
:reason "OK",
:content
{:results
[{:from_user "planetclojure",
:text "Removing defined tests in Clojure REPL
http://goo.gl/fb/qeUty #clojure #SO",
...
}, ...]
}
}
12. The Handler
(defn format-tweets [json] nil) ; todo
(defn handler [{{q "q"} :params}]
{ :content-type "text/html"
:body hiccup library function
(html [:body
(-> (search q) format-tweets)])})
same as (format-tweets (search q))
returns a map containing
:content-type and :body
19. ring server
% lein ring server
2011-04-16 21:18:54.965:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2011-04-16 21:18:54.968:INFO::jetty-6.1.26
2011-04-16 21:18:54.998:INFO::Started SocketConnector@0.0.0.0:3000
Started server on port 3000
% lein ring war
Created /Users/kurtharriger/code/twitturl/twitturl-1.0.0-SNAPSHOT.war
Amazon Elastic Beanstalk plugin
https://github.com/weavejester/lein-beanstalk
22. Finding Clojure
https://github.com/kurtharriger/twitturl
Leiningen Ring framework
DSLs Lists are awesome
Less () than Java De-structuring
Method chaining High-order functions
Kurt Harriger @kurtharriger