1. A very little bit of Clojure
Ben Stopford*
!
!
!
!
*(not a Clojure expert)
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. Lisps
•
LISP stands for LISt Processing, it’s a language of lists.
•
Form:
(function arg1 arg2 arg3)
(* 1 2 3) => 6
4. Lets look at a simple function
(defn four-of-kind? [hand]
(= 4
(first
(sort >
(map second
(frequencies
(map second hand)))))))
!
(four-of-kind?
[[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
5. Everything is a list
(defn four-of-kind? [hand]
(= 4
(first
(sort >
(map second
(frequencies
(map second hand)))))))
!
(four-of-kind?
[[:clubs 13] [:diamonds 8] [:hearts 8] [:clubs 8] [:spades 8]])
7. Map applies ‘second’ to the array of arrays ‘hand’
(defn four-of-kind? [hand]
(= 4
(first
(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. Frequencies computes number of occurrences
(defn four-of-kind? [hand]
user=> frequencies(…)!
(= 4
{8 4, 13 1}
(first
(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. ‘map second’ is used again to get the occurrences
(defn four-of-kind? [hand]
user=> (map
(= 4
(4 1)
(first
(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. Sort for the highest, take the first and see if it is 4
(defn four-of-kind? [hand]
(= 4
(first
(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. Terse but clear
(defn four-of-kind? [hand]
(= 4
(first
(sort >
(map second
(frequencies
(map second hand)))))))
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. 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)
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!
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 flow
•
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. 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. 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