Upcoming SlideShare
×

# A little bit of clojure

7,221

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
7,221
On Slideshare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
1
0
Likes
1
Embeds 0
No embeds

No notes for slide

### A little bit of clojure

1. 1. A very little bit of Clojure Ben Stopford* ! ! ! ! *(not a Clojure expert)
2. 2. Clojure • Clojure is a Lisp - the oldest programming language other than Fortran • Unusual syntax, stemming from polish notation • Functional, but not pure functional (like say Haskell) • JVM based and Dynamically Typed
3. 3. Lisps • LISP stands for LISt Processing, it’s a language of lists. • Form: (function arg1 arg2 arg3) (* 1 2 3) => 6
4. 4. Lets look at a simple function (defn four-of-kind? [hand] (= 4 (ﬁrst (sort > (map second (frequencies (map second hand))))))) ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
5. 5. Everything is a list (defn four-of-kind? [hand] (= 4 (ﬁrst (sort > (map second (frequencies (map second hand))))))) ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
6. 6. Preﬁx Notation (defn four-of-kind? [hand] (= 4 (ﬁrst (sort > (map second (frequencies (map second hand))))))) Start here ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
7. 7. Map applies ‘second’ to the array of arrays ‘hand’ (defn four-of-kind? [hand] (= 4 (ﬁrst (sort > (map second (frequencies (map second hand))))))) user=> (map second hand)! (13 8 8 8 8) ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
8. 8. Frequencies computes number of occurrences (defn four-of-kind? [hand] user=> frequencies(…)! (= 4 {8 4, 13 1} (ﬁrst (sort > (map second Hand has three (frequencies occurrences of (map second hand))))))) ;(13 8 8 8 8) “4” and one “7” ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
9. 9. ‘map second’ is used again to get the occurrences (defn four-of-kind? [hand] user=> (map (= 4 (4 1) (ﬁrst (sort > (map second (frequencies ;{8 4, 13 1} (map second hand))))))) ;(13 8 8 8 8) second (freq…! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
10. 10. Sort for the highest, take the ﬁrst and see if it is 4 (defn four-of-kind? [hand] (= 4 (ﬁrst (sort > (map second ; (4 1) (frequencies ;{8 4, 13 1} (map second hand))))))) ;(13 8 8 8 8) ! (four-of-kind? [[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
11. 11. Terse but clear (defn four-of-kind? [hand] (= 4 (ﬁrst (sort > (map second (frequencies (map second hand)))))))
12. 12. In Java, the signal to noise ratio is higher Card[] hand = {new Card("Diamond", 2), new Card("Club", 2), new Card("Heart", 2), new Card("Spade", 2), new Card("Diamond", 3)}; ! private boolean isNOfAKind(Integer n, Card[] hand) { Map<Integer, Integer> frequencies = new HashMap<Integer, Integer>(); for(Card card: hand){ if(!frequencies.containsKey(card.num)){ frequencies.put(card.num, 1); }else{ Integer frequency = frequencies.get(card.num); frequencies.put(card.num, ++frequency); } } List<Integer> counts = new ArrayList<Integer>(frequencies.values()); Collections.sort(counts, Collections.reverseOrder()); return n.equals(frequencies.get(0)); } static class Card { String suit; Integer num; public Card(String suit, int num) { this.suit = suit; this.num = num; }
13. 13. Lisps are notoriously slow 1m invocations of four-of-a-kind: • Java: 514ms • Clojure: 2,536ms Java is 5x faster (in this overly simple test)
14. 14. Why learn Clojure? “Learning a functional approach is good for your imperative programming”
15. 15. Why learn Clojure? Less bloat makes software easier to comprehend.
16. 16. Why learn Clojure? Runs on JVM so you can always drop back into the world you know
17. 17. Why learn Clojure? Immutability: ‘once you have written software with immutable data structures you won’t want to go back’
18. 18. Why learn Clojure? “The language of Language Makers” • Came from AI • Macros allow you to operate on code whilst it is still data so anything is possible. • Language changes are just additional libraries!
19. 19. Small but growing community
20. 20. My thoughts so far • Enjoying the complete rather than partial shift • Feedback cycle working in the REPL is awesome, TDD++ • You don’t loose your line of thought through typing / refactoring => easy to get ﬂow • Surprisingly elegant. • Huge amount of depth to it. I’ve only really scratched the surface • Still wondering how manageable a large code base would be?
21. 21. Hacker and Painters “All makers have the same problem: there is not much money to be made on things fun to work on. One answer is to have a day job, as painters do.”
22. 22. Easy to get started with • Lein (think Maven with less xml) • Eclipse and Intelij plugins • Free books: • Pragmatic Programmers “Programming Clojure” • Clojure section of 7 languages in 7 weeks • Structure and Interpretation of Computer Programs • http://www.4clojure.com/ (learning problems site) • The Little Schema, learn by example
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.