Download presentation
Presentation is loading. Please wait.
Published byFlora Williams Modified over 9 years ago
1
Why Clojure? Adrian Miron
2
Using Java since Found both on a CD from Professionally (getting payed :-) since 2001 Back then I knew nothing
3
Clojure is a Lisp dialect Lisp is old… ’58 Runs on the JVM Production ready, general purpose, functional Opinionated Read: build to please Rich Hickey :-)
4
(defn words [text] (re-seq #"[a-z]+" (.toLowerCase text))) (defn train [features] (reduce (fn [model f] (assoc model f (inc (get model f 1)))) {} features)) (def *nwords* (train (words (slurp "big.txt")))) (defn edits1 [word] (let [alphabet "abcdefghijklmnopqrstuvwxyz", n (count word)] (distinct (concat (for [i (range n)] (str (subs word 0 i) (subs word (inc i)))) (for [i (range (dec n))] (str (subs word 0 i) (nth word (inc i)) (nth word i) (subs word (+ 2 i)))) (for [i (range n) c alphabet] (str (subs word 0 i) c (subs word (inc i)))) (for [i (range (inc n)) c alphabet] (str (subs word 0 i) c (subs word i))))))) (defn known [words nwords] (let [result (set (for [w words :when (nwords w)] w))] (if (empty? result) nil result))) (defn known-edits2 [word nwords] (let [result (set (for [e1 (edits1 word) e2 (edits1 e1) :when (nwords e2)] e2))] (if (empty? result) nil result))) (defn correct [word nwords] (let [candidates (or (known [word] nwords) (known (edits1 word) nwords) (known-edits2 word nwords) [word])] (apply max-key #(get nwords % 1) candidates)))
5
~2009-2010 I was looking for a greener field Kinda like a programmer midlife crisis Because all my code was full of for’s and Map’s and List’s and I was shoving data in and out of them using tons of syntax for almost nothing Everything seemed leaky I was looking at Scala, F# and Clojure Clojure won
6
Most influential Out of the Tar Pit Are we there yet – a talk by Rich Hickey On Understanding Data Abstraction, Revisited Lots and lots of articles about the downsides of OOP The cornerstone Simple made Easy – a talk by Rich Hickey at Strangeloop
7
Clojure is designed from the ground up to manipulate data Which is what most programs do A basic set of data types and data structures Integer, Double, BigDecimal, Ratio, String, Character, Symbol, Keyword, Boolean, Nil list (), vector [], map {}, set #{} Lots and lots of functions that can manipulate them (->> persons (map :height) (filter #(> % 10)) (reduce +))
8
All data structures are immutable They are persistent data structures There is no copy-on-write when you create a new structure from an old one Represented as shallow trees Marketed complexity for operations is O(1) In reality is O(log32 n) Because the nodes have a length of 32
9
Clojure is not OO Which is a plus IMO Here’s a secret: Objects are not that good for programming :D Leaky Objects hide data behind a specific “interface” for every single type Combine data and behavior Combine identity and state Combine inheritance and polymorphism How do you solve problems then? ;) Data and functions Grouped in namespaces
10
Clojure has specific constructs to deal with state and processes Immutability No update-in-place Well defined semantics of how state evolves over time Based on observing a succession of values Values are propagated via channels (core.async) Like queues with additional semantics Based on CSP (Communicating Sequential Processes)
11
Clojure is a dynamic language Dynamically typed Runtime Offers much more freedom of doing things that are impossible in Java Riemann as an example for code as configuration “Shallow” (my terminology) statically typed languages like Java do not offer enough advantages for forcing a type system Most of errors caught by types can also be caught by basic testing
12
Clojure is design to be easy to reason about the code Does that by Less verbose Immutable Functional Less LOCs Easier to test
13
References Out of the Tar Pit On Understanding Data Abstraction, Revisited Are We There Yet? Simple Made Easy Clojure Made Simple
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.