Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clojure: An Introduction

Clojure: An Introduction

From FinJUG meeting March 2013. Introduction to Clojure.
http://finjug.org/

Tero Parviainen

March 13, 2013
Tweet

More Decks by Tero Parviainen

Other Decks in Technology

Transcript

  1. Clojure is a dynamic, functional, general purpose programming language for

    the JVM. dynamically typed, extensible, interactive
  2. Clojure is a dynamic, functional, general purpose programming language for

    the JVM. dynamically typed, extensible, interactive programming with functions, emphasis on immutability
  3. Clojure is a dynamic, functional, general purpose programming language for

    the JVM. dynamically typed, extensible, interactive programming with functions, emphasis on immutability “Use it anywhere you would use Java”
  4. Clojure is a dynamic, functional, general purpose programming language for

    the JVM. dynamically typed, extensible, interactive programming with functions, emphasis on immutability “Use it anywhere you would use Java” runs on the JVM, designed for the JVM
  5. OOP talk() String name int age Animal fetchBall() String breed

    Dog purr() String breed boolean grumpy Cat
  6. The “Anemic Domain Model” String number String owner String bank

    int balance Account String firstName String lastName String number String ssn int yearStarted Employee String name String code int inventory Product String name String code String address String tel Customer DateTime timestamp String clerk int totalAmount int vatAmount Receipt
  7. VerbEscorters action() Controller calculate() Calculator build() Builder validate() Validator makeImageTag()

    makeForm() Helper load() save() Service create() delete() Manager formatTime() formatNumber() getVatClasses() Util
  8. Steve Yegge Verbs in Javaland are responsible for all the

    work, but as they are held in contempt by all, no Verb is ever permitted to wander about freely. If a Verb is seen in public at all, it must be escorted at all times by a Noun. Of course “escort”, being a Verb itself, is hardly allowed to run around naked; one must procure a VerbEscorter to facilitate the escorting. http://steve-yegge.blogspot.fi/2006/03/ execution-in-kingdom-of-nouns.html
  9. Rich Hickey Object-orientation is overrated. [It was] born out of

    simulation, [but is] now used for everything, even when inappropriate http://clojure.org/rationale
  10. Data List Vector Map Set (1 2 3 4) [1

    2 3 4] {:name “Joe”, :parents [:jane :bob]} #{:one :two :three :four}
  11. Functions conj assoc dissoc first rest nth peek pop keys

    vals empty? every? some cycle map reduce filter remove concat mapcat interleave interpose group-by partition split-at zipmap take drop
  12. Steve Yegge Classes are really the only modeling tool Java

    provides you. So whenever a new idea occurs to you, you have to sculpt it or wrap it or smash at it until it becomes a thing, even if it began life as an action, a process, or any other non-”thing” concept. I’ve really come around to what Perl folks were telling me 8 or 9 years ago: “Dude, not everything is an object.” http://steve-yegge.blogspot.fi/2006/03/ execution-in-kingdom-of-nouns.html
  13. Syntactic Patterns InputStream in = file.openStream(); try { // ...

    } catch (IOException ioe) { // ... } finally { try { in.close(); } catch (IOException ioe) { // ... } }
  14. ( defn all-valid? [records] ( every? empty? ( map :errors

    records ) ) ) Data List Symbol Vector List List Keyword
  15. Paul Graham If you understand how compilers work, what’s really

    going on is not so much that Lisp has a strange syntax as that Lisp has no syntax. You write programs in the parse trees that get generated within the compiler when other languages are parsed. But these parse trees are fully accessible to your programs. You can write programs that manipulate them. In Lisp, these programs are called macros. They are programs that write programs. http://www.paulgraham.com/avg.html
  16. org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize

    (AbstractLazyInitializer.java:86) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation (AbstractLazyInitializer.java:140) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke (JavassistLazyInitializer.java:190)
  17. Stateless Functions conj assoc dissoc first rest nth peek pop

    keys vals empty? every? some cycle map reduce filter remove concat mapcat interleave interpose group-by partition split-at zipmap take drop
  18. Rich Hickey One could argue that systems with manual locking

    are usually broken, and therefore their behavior itself, never mind their performance, is unpredictable. That’s been my experience with manual locking in the hands of mere mortal developers. http://www.azulsystems.com/blog/cliff/ 2008-05-27-clojure-stms-vs-locks
  19. Immutable Data Structures List Vector Map Set (1 2 3

    4) [1 2 3 4] {:name “Joe”, :parents [:jane :bob]} #{:one :two :three :four}
  20. ...And Stateless Functions conj assoc dissoc first rest nth peek

    pop keys vals empty? every? some cycle map reduce filter remove concat mapcat interleave interpose group-by partition split-at zipmap take drop
  21. ...And Managed References (def bob (ref 10)) (def joe (ref

    50)) (defn transfer [from to amount] (dosync (alter from - amount) (alter to + amount))) (transfer bob joe 5)
  22. Ron Garret The Remote Agent software, running on a custom

    port of Harlequin Common Lisp, flew aboard Deep Space 1, the first mission of NASA’s New Millennium program. Remote agent controlled DS1 for two days in 1999. During that time we were able to debug and fix a race condition that had not shown up during ground testing. Debugging a program on a $100M piece of hardware that is 100 million miles away is an interesting experience. http://www.flownet.com/gat/jpl-lisp.html
  23. (defn checknum [n] (if (zero? (rem n 2)) "even" "odd"))

    public interface IFn extends Callable, Runnable java.lang.String long
  24. Data & Behavior: Separate Syntax: Extensible State: Mostly Unnecessary State

    & Concurrency: Managed Workflow: Continuous Java: As Platform