The post provides a description of some of key concepts and words used in Clojure.
Let binds symbols to immutable values
(let [a 7
(+ a b)
The bound symbols are available in lexical scope, i.e. a and b can’t be accessed outside.
Multi-arity functions is same as overloading methods in C# which means varying number of arguments can be passed to the same function name
( (addNumber 2 3))
([a b] (+ a b)))
(addNumber 20 30) 50
Variadic arity Function
Variadic arity allows one version of a function with variable number of arguments.
It uses the & symbol in parameters which means collects all remaining args
(defn concatString [a & b]
(print a b))
(concatString "I" "M" "N" "P")
Apply invokes function on arguments
(let [a 1
more '(3 4)]
(apply concatString a b more))
Apply can be used with operators also
(let [numbers '(1 2 3)]
(apply + numbers))
Allows to access properties
Chaining Function using Clojure Sugar
Let’s say we need to get first name of the object employee. We might have to get full name of the employee before working out the first name.
Using C# the syntax can be something like
(.getFirstName (.getName employee))
The code in Clojure is not easy to read. Using Clojure Sugar (..) the code can be written as
(.. employee getName getFirstName)