https://gist.github.com/jjcorrea/12780b88ca38b5490abe
http://www.infoq.com/presentations/hickey-clojure
http://learnxinyminutes.com/docs/clojure/
- Emphasis in immutability and referential transparency (no side-effects)
- Explicit concurrency support
- Designed for the JVM
- Code as data
- Dynamically typed, and compiled
- Interactive - REPL
- Load / Change code in running program
- Arbitrary precision integers
123456
- Doubles
1.234
- BigDecimals
1.234M
- Ratios
12/7
- Strings
"sample"
- Chars
\a \b \c
- Symbols
sample
- Keywords
:sample
- Booleans
true false
- Regex
#"a*b"
-
Lists - Singly linked, grow at front
(1 2 3 4 5) (sample1 sample2 sample3) (list 1 2 3)
-
Vectors - Indexed (fast) access, grow at the end
[1 2 3] [sample1 sample2 sample3]
-
Maps - Key-Value associations
{:a 1 :b 2 :c 3} {1 "sample1" 2 "sample2"}
-
Sets
#{sample1 sample2 sample3}
Things that would be declarations, control structures, function calls, operators, are all just lists with the op at the front
- Data structures are the code
- Not text-based syntax, syntax is the interpretation of the data structures
- No statements, everything is an expression
- Tiny core, extended with Macros
Functional programming as easy as programming in Python or Ruby
- Immutability
- Core library functions have no side effect
- Easier to understand
- Easier to test
- Higher order functions
- Sequences
- Maps and Sets
- Immutable - Making a new version is cheap
- All clojure data structures are persistent
- Structural sharing - Thread safe, iteration safe
- Conventional way: Direct references to mutable objects with Locks
- Clojure way: Indirect references to immutable and persistent data structures. No locks in code.
The only thing that modifies a reference is the reference itself, in a controlled way
3 types of mutable references:
-
Refs Share Synchronous changes between threads - Transactional system
-
Agents Share Asynchronous changes between threads - Like actor models, but doent require message passing to read values
-
Vars Isolates changes within threads - Thread local
- Software transactional model system (STM)
- Refs can only be changed within a transaction
- All changes are atomic, consistent and isolated
- Will be retried automatically if conflict
- User must avoud side-effects
- Surround code with
(dosync ...)
- Agents are not actors (Erlang / Scala)
- Independent state
- Actions = Functions
- Actions occur asynchronously, in a thread pool, and are dispatched using send or send-off which returns immediately
- One action per agent at a time