; and continue to the end of the line. By convention, a single ; is a comment for the same line. A ;; denotes a block within a definition. A ;;; denotes a section within a source file.
of parameters it expects. It is an error to invoke a function with the wrong number of arguments. user=> (=) clojure.lang.ArityException: Wrong number of args (0) passed to: core/=
to other things. The Var prints itself as #'user/book. A side effect is that the current namespace, user, is modified: a new Var is created and added to the namespace.
functions. • A function is pure if it performs no side-effects. • A pure function must not reference mutable state. • Input or output of any kind is impure: • Reading from the keyboard • Reading from a file, socket, etc. • System.currentTimeMillis(), etc. • Randomness is impure.
to be simpler. You don’t have to think about anything outside the function’s body and parameters. You can test them easily, and experiment with them using the REPL. Pure functions compose better than impure functions.
threads, it does not matter what threads are doing which calculations. Computing the result of a pure function can be deferred until the value is needed. The result can be cached, using the parameters as a key. This is called memoization.
fields whose value can be updated at any time. Mutability works against purity. Basically, any mutable field anywhere is an additional invisible parameter of the Java method. This makes reasoning about Java much harder.
iterate keeps passing a value through a function, building an infinite list from it, as if: (1 (inc 1) (inc (inc 1)) (inc (inc (inc 1))) …) Clojure handles infinite data really well via lazy evaluation.
collection value is passed, in turn, to the predicate. Using filter, only the truthy predicate results are included in the result. remove is the opposite; truthy predicate results are removed, falsey are retained.
as lazy values are computed. A lazy collection’s externally visible state is pure, as long as the mapping function (for map) or predicate (for filter and reduce) is pure. Of course, the values in the collection need to be pure as well.
"Charles" "Brendan") A keyword can act as a function for accessing a value from a map. Remember that map is a data structure, and map is a function for transforming collections.
input to the next. (->> coders (map :name) (map #(subs % 0 1)) set) ➠ #{"G" "R" "C" "B"} subs extracts a substring from a string. set converts a collection to a set. Notice how "B" is only present once.
value: 1. Update the map, using the value as a key 2. Increment the map value (the count) 3. When the map value is nil, use 0 as default (reduce (fn [m v] (update m v (fnil inc 0))) {} [1 3 5 10 5 3])
be divided into two parts: some set of fundamental operators that play the role of axioms, and the rest of the language, which could in principle be written in terms of these fundamental operators.\n") (def sep #"[\p{Space}|\p{Punct}]+") (->> (str/split text sep) (map count) frequencies) ➠ {7 2, 4 6, 6 1, 3 8, 2 8, 11 3, 9 3, 5 5, 8 2}