• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Clojure Small Intro
 

Clojure Small Intro

on

  • 1,594 views

Presentation for JHUG in Greece

Presentation for JHUG in Greece

Statistics

Views

Total Views
1,594
Views on SlideShare
1,581
Embed Views
13

Actions

Likes
1
Downloads
18
Comments
0

3 Embeds 13

http://www.linkedin.com 10
http://paper.li 2
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Clojure Small Intro Clojure Small Intro Presentation Transcript

    • Clojure John Vlachoyiannis @jonromero jon@emotionull.comHttp://jon.is.emotionull.com
    • What is Clojure?
    • Ok, what is Lisp?
    • “Lisp is worth learning for the profound enlightenment experience you will havewhen you finally get it; that experience willmake you a better programmer for the restof your days, even if you never actually use Lisp itself a lot." Eric S. Raymond, "How to Become a Hacker".
    • “LISP stands for: Lots of Insane Stupid Parentheses” Anonymous
    • The Truth about Lisp
    • LISt Processing LIS● Second oldest high-level language (first is Fortran)● Code as Data (Homoiconic)● Perfect for Domain-specific languages (DSL)● Exploratory programming
    • Clojure● Lisp in JVM● Concurrent programming● Dynamic Development (REPL)● Lazy sequences● No side effects (almost)
    • Enter Clojure
    • Everything is code
    • (println "Hello World")function argument
    • Everything is data
    • (println "Hello World")list symbol string
    • ● Integers – 1234567891234● Doubles – 3.14. BigDecimals – 3.14M● Ratios – 22/4● Strings – ”foo”, Character – a b c● Symbols – foo, Keywords :foo● Booleans – true false, Null – nil● Regex patterns – #”[a-zA-Z0-9]|
    • Data structures● Lists (1 2 3 4), (foo bar baz), (list 1 2 3) ●● Vectors [1 2 3], [foo bar], (vector 1 2 3) ●● Maps {:a 1 :b 2 :c 3} ●● Sets ● #{foo bar}
    • “It is better to have 100functions operate on one data structure than to have 10 functions operate on 10 data Structures.” Alan J. Perlis
    • clojure might be a better java than java
    • public class StringUtils { public static boolean isBlank(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) ==false)) { return false; } } return true; }}
    • public class StringUtils { public isBlank(str) { if (str == null || (strLen = str.length()) == 0) { return true; } for (i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) ==false)) { return false; } } return true; }}
    • public isBlank(str) { if (str == null || (strLen = str.length()) == 0) { return true; } for (i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) == false)) return false; } } return true;}
    • public isBlank(str) { if (str == null || (strLen = str.length()) == 0) { return true; } every (ch in str) { Character.isWhitespace(ch); } return true;}
    • public isBlank(str) { every (ch in str) { Character.isWhitespace(ch); }}
    • (defn blank? [s] (every? #(Character/isWhitespace %) s))
    • Clojure vs Java code● Side-effect free ● Error prone● Easy to (unit) test ● Not so easy● Lazy collection ● Only one element● Any element ● Only with chars● Slower ● Faster● Data manipulation ● If/for/while code● Exploratory ● Design Is Law
    • Clojure is a functional language● Functions are first-class objects● Data is immutable● Functions are pure
    • So what?
    • ● Simple: no loops, variables or mutable state● Thread-safe: no locking● Parallelizable: map/reduce anyone?● Generic: data is always data● Easy to test: same input, same output
    • user=> (println "hello world")| hello world-> nil
    • user=> (defn hello [name] (str "Hello, " name))#user/hello
    • (hello "Clojure")
    • (.toUpperCase “hello”)
    • (str “hello” “ “ “world”)
    • (+ 1 3 4 (* 5 6))
    • (defn greeting"Returns a greeting of the form Hello, username."[username](str "Hello, " username))
    • (greeting "world")
    • user=> (doc greeting) ------------------------- exploring/greeting ([username])Returns a greeting of the form Hello, username.
    • (defn is-small? [number] (if (< number 100) "yes" ))
    • (is-small? 50) "yes"
    • (is-small? 50000) nil
    • (defn is-small? [number] (if (< number 100) "yes" "no" ))
    • Solving problems● Experiment with the problem● Create your data structures● Data transformations● Write code that writes code for you (macros)● Create a mini language (a DSL)
    • Java in Clojure● Yeap
    • Clojure in Java● Yeap
    • Clojure in Clojure● Yeap, yeap
    • Java in Clojurejava new Widget(“foo”)clojure (Widget. “foo”)java dialog.show()clojure (.show dialog)
    • Tools● Emacs + SLIME● ViMClojure● Enclojure + Intellij IDEA● Counterclockwise + Eclipse● Leiningen
    • Clojure is not a just a new language
    • Is another, simplier way to solve problems
    • Thanks! Questions? @jonromero