Introduction to Clojure and why it's hot for Sart-Ups
1.
2. DISCLAIMER:
1 YOU MIGHT ALREADY KNOW!
2 I AM NOT A ‚TYPOLOGIST‘
TAKE IT AS A JOURNEY WITH
ANOTHERS EYES
3. OVERVIEW
• Teil 1: Prolog und Philosophie
• Teil 2: Core Language Concepts
• Teil 3: (Language Specifics)
• Teil 4: Epilog
Recently Cedric Beust said something like:
„Whenever there is a new paradigm or language
learn from it. Don‘t fall in love.“
POLY
5. MY HISTORY…
• 1980-1990
• Prof. Dr. Peter Pepper
• Language OPAL
• Tutor
“Opal is a strongly typed, higher-order,
strict, pure functional language, and as
such can be classified alongside ML,
Haskell, and other modern functional
programming languages. However, the
language also has a distinctive algebraic
flavour in the tradition of languages such
as CIP-L, Obj, and others.”
12. INSIGHTS FROM THE PAPER
• OOP re-use does rarely work!
• komplexity increases exponentiell („boot it…!“)
• We „humans“ are not able to master many states
Solution:
• No State! = No Vars!
• less code (e.g. powerful comprehensions)
• Higher-Order Functions
• everything super abstract
13. FIRST LANGUAGE FROM A 3RD PARTY
PHILOSOPHY!
Together with Scala strong market penetration in Big-Data (Analytics)
• BigML
• Storm
• Cascalog
• Nokia
• Twitter
• Akamai
• Factual
• Prismatic!!
• Groupon
• Heroku!! As PaaS!
• Top 1 TechRadar von ThoughtWorks (with Scala)
15. CLOJURE TOP #12
1. Functional => pure / first-class / Higher Order Functions
2. Immutable => safe and correct Functions
3. Super Expressiveness => smart with fewer Brackets
4. DSL ready (e.g. parsley)
5. OO or logic concepts easy to integrate (core.logic)
6. Java hosted => fast & libs
7. perfect Java Integration & integration easier then Scala (at least to me)
8. Multicore Ready / multi-CPU with MVCC STM=> always Thread Safe
9. Strong Macros (Scala is currently integrating this) => dream a language
10. dynamic and strong typed (to me d, to vm s)
11. super abstract Programming (composable)
12. Tons of Books, Docs, Links, Videos, Trainings, etc.
17. Performance Tipps:
- use native Types & generic collections
- use the array wrapper of Clojure
- use Java
18. LESS CODE = LESS ERRORS
Best Expressiveness (rosettacode.org)
73 C#
41 Clojure
115 D
202 Go
68 Groovy
88 Java
26 Prolog .. ups?? (how can this be?)
83 Python
100 Ruby
129 Scala
Core logic does it in 33 lines:
http://dosync.posterous.com/friendlier-shorter
19.
20.
21. NO MUTABLE STATE = NO VARS
INPUT
files, dbs, user
Advantage: Functions can be pure! Disadvantage: sometimes hard
23. Neil Ford, Robert „Bob Martin“, Howard Lewis Ship,…
„Clojue is a dynamic, strongly typed,
functional, high-performance
implementation of a lisp on the JVM.“
NF on WJax
27. ABSTRACIONS EVERYWHERE
The world consists of Lists! => Sequences
• or even lazy Lists! (lazy sequences)
Even Trees and Graphs are lists
Mostly Closuje doesn„t care if it gets
• a List `()
• a Vector []
• a (sorted) (hash) Map {}
• a (sorted) Set #{}
29. CEREMONY YIELDS CEREMONY
package a.c.b
public class Person {
public / private / protected vars …
private vars …
protected vars …
constructors …
getters / setters …
visibility public / private / protected methods …
attributes
getters setters
constructors • All mixed together!
namespaces • Leads to more code!
methods • Leads to more accidential complexity!
and more… • Leads to unneccesary Design Patterns!
30. List
Map
Sequence + A function
Set
MOSTLY INHERENT COMPLEXITY
31. MDD : MAP DRIVEN DESIGN
This leads partially to a different design approach:
• Identify problem domains
• Start with defining your data in terms of maps,lists, seqs,…
• The protocol can be the data
• Not the API in terms of functions which are already bound to
Classes and Types!
• Connect these domains loosely by passing data
• e.g. with pipes / queues / events / etc.
Sam Aaron:
„A map key often speaks more and
better then a documented interface / class“
32. DESIGN PATTERNS VANISH!
Peter Norvig
demonstrated some
time ago
http://www.norvig.com/d
esign-patterns/ that
Lisps in particular either
simplify or make
invisible most design
patterns!
33. HIGHER ORDER FUNCTIONS
MACROS
comp
partial, apply
map, reduce, sort,
filter, super-for, range
repear, iterate, drop, cycle
interleave, interpose, take, split
some?, not-any?, not-every?, …
abstract continue for new switch und 1000 mehr
assert default if package synchronized
boolean do goto private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch
char
extends int
final
short
interface static
try
void
def, if, do, let, ´, fn, loop, recur,…
class finally long strictfp volatile
const float native super while
Lists, Lambda Kalkül
Keywords: imparativ
34. CONCLUSION PART II
• Clojure is a cleaned up lisp
• introduction of a sequence abstraction
• powered by the JVM
• easier symbol resolution / syntax quoting
• convenient literal syntax => less parantheses
• params in a vector
• Commas are whitespaces in data
• Avoid many nestings…
(cond ((< x 10) „less“) (cond (< x 10) „less“
(> x 10) „more“)) (> x 10) „more“)
36. CLOJURE AREAS
Datatypes
Functions
map, reduce,
Java Interop pmap
Control
Sequences / sequable
map / reduce / reduce++
Laziness
Concurrency / Reference Types (STM) Uses fork / join for easy
seq processing on all
Datatypes & Protocols cores you have.
Multimethods
Macros
37. MAPS ARE FUNCTIONS
KEYS ARE FUNCTIONS
* IS A FUNCTION
> (def edlich {:name „edlich“ :age 42})
> (edlich :age) => 42
> (:age edlich) => 42
38. HAMMOCK DRIVEN DEVELOPMENT - HDD
http://blip.tv/clojure/hammock-driven-development-4475586
Don„t move your fingers fast…
Think & Sleep!
39. 4CLOJURE
• Not a competition about the shortest
unreadable code
• but shorter code!
• more consise code!
• craftable code! „there is a simpler solution“
• more abstract code!
see http://tapestryjava.blogspot.de/2013/02/crafting-code-in-clojure.html
44. BETTER REPL THEN *
Always run & change your env...! No
interpretation. Recompile & run in no time.
Idea: Bret Victor “Inventing on Principle”
http://www.youtube.com/watch?v=PUv66718DII
45. FOR / LOOPS = A CODE SMELL!
Do a recursive reverse withouth ‚reverse„
95% no loop needed!
46. WHY GOOD 4 STARTUPS
• Truly Composable
• Fast
• Correct
• Concurrent Access Solved: Atoms, Refs & Agents (Actors e.g. via Akka)
• Multicore:
• Lazy Sequences / Caching / Old Reducers
• New Reducers
fold combine
fork
.
.
.
join
} like Map /
Reduce split
and calc on N
cores
47. DRAWBACKS?
• Too many brackets? Random Code from Apache Commons
(defn blank? [s] (every? #(Character/isWhitespace %) s))
48. DRAWBACKS?
• Eclipse, IntelliJ, NetBeans, IDEs are good but far from JavaIDEs
• Many reasons for a IDE vanish (lucky with emacs, vim, sublimetext)
• Debugging is not mature
• visual VM is ok. By the way we do not debug state!
• We copy snippet in REPL and have the solution!
• Static is more safe? Type mismatch is rare
• Leiningen Build 2.0 is ok (uses Maven internally and feels like Maven)
• List add Back, Vector add front => because…
• Don„t mix abstract and concrete world
• Namespaces are hard
• But super powerful…
49. THEN WHEN HAVING A BEER HE TOLD ME:
„Well since 2010 I am working on a database!“ ...whow
51. STEFAN KAMPHAUSEN
Chapter 1.3
Ein Wort der Warnung ist angebracht. Die Benutzung von Lisp-
artigen Programmiersprachen kann süchtig machen und / oder die
Entwicklung mit anderen Sprachen unangenehm bis schmerzhaft
erscheinen lassen. Sollten Sie sich nach der Lektüre dieses
Buches und ggf. einiger Projekte nicht mehr in der Lage sehen, mit
anderen Sprachen zu arbeiten, können wir dafür keine
Verantwortung übernehmen.“
(It‘s time for a warning. The usage of Lisp-alike languages can lead
to addiction. The usage of other language then becomes awkward
and painful. We can not take any responsibility if you – after
reading this book – become unable to work with other languages!)
52. QUOTES…
„Clojure does not feel like hacking. It feels like a very analytical
precision surgery of a problem into its component parts. You can
write big, ugly Clojure. But good Clojure is readable and
maintainable, and a pleasure to write in.” Eric Normand
“If you try to build abstractions in Java code it tends to get bigger. If
you write abstract code in Clojure it tends to get shorter.”
“The other day, I was working on a little bit of code in Clojure, just
touching up some exception reporting, when I was suddenly struck
by one of the fundamental reasons that Clojure is so enjoyable to
code in. Clojure is craftable: that is, in Clojure you have the option to
craft at your code to make it more concise, easier to read, and easier
to maintain. That is not the case for all, or perhaps even most,
programming languages.” Howard Lewis Ship (Tapestry)
http://tapestryjava.blogspot.de/2013/02/crafting-code-in-clojure.html
and others converted as Uncle ‚Bob‘ Martin, Neil Ford, etc…