0

Clojure in 10 Minutes Clojure Meet-Up Copenhagen September 23, 2010 Martin Jul, mj@ative.dk / @mjul

What is Clojure? • Lisp • Functional Programming • Immutable, persistent data structures • Software Transactional Memory • Object-oriented (sort of) • Macros • Managed code: JVM and CLR Clojure is the future! -- Uncle Bob Getting off the Lisp Island

Syntax and Data Types ; This is a comment, semicolon is similar to // in C# / Java ;; List is the most common data structure '(1 2 3) ;; Vector is a list that is indexable by position [1 2 3] ;; Maps are associative data structures {:key "value", :id 42} ;; Sets are mathematical sets #{1 2 3} ;; Expressions are lists of form (function arg-1 arg-2 ...) (println "Hello, World") (+ 1 2 3) file: core.clj List Vector Map Set Expression

Clojure Basics file: functional.clj Incidentally this could also have been a modem connect string

Functional Programming file: functional.clj

Simple and Concise (defn blank? [s] (every? #(Character/isWhitespace %) s)) Source: Programming Clojure by Stuart Halloway (Pragmatic Programmers, 2009)

”State – you’re doing it wrong” • Immutable • Persistent (struktural sharing, not copying) b (def a (list 1 2 3)) (def b (rest a)) a 1 • Simpler code • Less concurrency issues 2 3 e.g. stable enumerations

STM – Software Transactional Memory ref 1 1 2 file: stm.clj concurrency-safe in-memory transactions!

Object Oriented with Protocols (price-with-vat espresso) (vat iver) file: oo.clj

Multi-methods Not your mother’s dispatch file: multi.clj

The Clojure Compiler Text Clojure data structures (read stream) Reader AST Macro evaluation (eval ast) Compiler is available at runtime Byte code AST Compiler Your hip friends call this language homoiconic Great for DSLs

Example: Compojure web app (ns hello-world (:use compojure.core, ring.adapter.jetty) (:require [compojure.route :as route])) (defroutes main-routes (GET "/" [] "<h1>Hello World</h1>") (route/not-found "<h1>Page not found</h1>")) (run-jetty main-routes {:port 8080}) Source: http://github.com/weavejester/compojure shorter than your web.config!

1 Publizr (dev)

Index

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
Home


You need flash player to view this online publication