@Clojure
- Is a dialect for Lisp, Lisp is a special langauge.
- Is homoiconic with almost no syntax.
- Has a full macrosystem also supports metaprogramming.
- Is a functional language with first-class functions.
- Immutable data structures.
- Inbuilt concurrency semantics.
- Hosted on the extremely mature and performant JavaVM.
- Seamless interoperability with Java code.
- General purpose as well as dynamic programming language.
- Excellently suited for domain-specific langaue style of programming.
@Why Clojure?
- A Lisp
- For functional programming
- Symbiotic with an established Platform
- Designed for Concurrency
- Code as data.
@Clojure unique features
- Dynamic Development.
- Functional Programming.
- Lisp.
- Runtime Plymorphism.
- Concurrent Programming.
- Immutability.
- Higer-order functions.
- Laziness.
- Excellent concurrency semantics.
- Software Transaction Memory.
- Hosted on the JVM
@Clojure program structure
- Literals
- Strings
- Characters
- nil
- true
- false
- integers
- floating point numbers
- ratios
- Vectors
- Lists
- Maps
- Sets
- Symbols
- Special Forms
- def
- if
- do
- let
- quote
- var
- fn
- loop
- recure
- throw
- try
- monitor-enter
- monitor-exit
- other special forms
- dot(.)
- new
- set!
- Composite Forms
- Function Calls
- Macros
@Clojure and OOP Paradigm
- Modularity => Clojure namespacing mechanism
- Polymorphism => Clojure Multimethods
- Encapsulation => Clojure functions
- Reusability => Clojure functions
@Function
A function, is a mathematical sense, expresses the idea that one quantity
(the argument of the function, also known as the input) completely
determines another quantity (the value, or the output).
@Clojure Functions
In Clojure, all functions are first-class objects.
- Can be dynamically created at any point during the execution of the program.
- Are not intrinsically named, can be bound to symbols.
- Can be stored as value in any data structure.
- Can be passed to, and returned from, other functions
@Closure Oriented Programming
- Functions as parameter
- Functions as return value
- Composing functions
@Anonymous Functions
Three reasons to create the anonymous functions
- The function is so brief and self-explanatory that giving it a name makes the code harder to read, not easier.
- The function is beign used only from inside another function and needs a local name, not a top-level binding.
- The function is created inside another function for the purpose of closing over some data.
@Clojure coding guidelines
- Start every source file with a namespace declaration using ns.
- Use :import and :use expression in ns to describe the classes and namespaces it depends on.
- Always use :only option of :use to make it clear which symbols you need from the other namespace.
- Reuse good names, add the :refer-clojure expression to ns if needed.
- Structure your source files to avoid the need for forward declarations.
Place "primitive" definations near the top and the "composite" definations that depend on them toward the bottom.
- Use let to make your code cleaner and to store the results of calculations, so you don't have to perform them multiple times.
- Use refs provide synchronous, coordinated updates, and allow direct access to the STM System
- Use atoms to manage synchronous, independent state (such as chahed or memorized values) with maximum efficientcy.
- Use agents to manage asynchronous state as well as introduce concurrency into your program.
- Use vars to maintain state within a stack discipline to efficiently simulate mutable variables for algorithms that require it.
- Use validator functions to maintain data integrity.
- Use watches to trigger events dependent on an identitys values.
- Use the anonymous function only when you find that they make your code more readable.
- Use the metadata reader macor to add metadata to vars and parameters.
- Use with-meta to add metadata to data.
@Reference
- Practical Clojure by Luke VanderHart and Stuart Sierra
- Clojure In Action - Amit Rathore
- Programmin Clojure - Stuart Halloway