Ch 6 + 0.5 of Ch 7 of Brave Clojure
Jul 10, 2020
Contents
Pascal’s Triangles
In mathematics, Pascal’s triangle is a triangular array of the binomial coefficients.
For example -
[1]
[1 1]
[1 2 1]
[1 3 3 1]
[1 4 6 4 1]
The 4Clojure problem asks you to create a function which returns the nth row of Pascal’s Triangle. I tried solving it before with map and reduce but it didn’t work out.
Today, I decided to give the problem another shot. And lo and behold, a solution came to me in about 5 minutes.
(defn pascalize
([n] (pascalize (dec n) '(1)))
([n p]
(if (zero? n)
p
(recur (dec n) (conj (map #(apply + %) (partition-all 2 1 p)) 1)))))
Dead simple. I think this was the first time I instinctively reached for a loop recur construct to solve a problem.
Chapter 6
map and inc are symbols
When you give clojure a symbol like map, it finds the corresponding var in the current namespace, gets a shelf address, and retrieves an object from that shelf for you.
if you want to use the symbol itself, quote it
'inc
=> inc
'(map inc [1 2 3])
=> (map inc [1 2 3])
(def great-books ["Sherlock Holmes" "Alice in Wonderland"])
=> #'clojure-noob.ch6/great-books
great-books
=> ["Sherlock Holmes" "Alice in Wonderland"]
Interning a var
- Updates the current ns map with the association between great-books and the var
- Stores the vector on a free shelf spot
- Returns the address of the shelf on the var Which is
#'clojure-noob.ch6/great-books
- use (ns-interns ns) to see the namespace’s map
(get (ns-interns *ns*) 'great-books)
#'clojure-noob.ch6/great-books -> reader form of a var
;; use #' to grab ahold of the var corresponding to the symbol
(deref #'clojure-noob.ch6/great-books)
;; => ["Sherlock Holmes" "Alice in Wonderland"]
(def great-books ["The power of bees" "Journey to Upstairs"])
great-books
;; => ["The power of bees" "Journey to Upstairs"]
- create-ns -> take a symbol, creats a namepsace with that name if it doesnt exist already and returns the namespace
(create-ns 'cheese.taxonomy)
=> #namespace[cheese.taxonomy]
You can use the returned namespace as an argument in a function call
(ns-name (create-ns 'cheese.taxonomy))
=> cheese.taxonomy
- in-ns -> creates a namespace and switches to it
- use the fully-qualified name to refer to vars and fns from a different ns
- refer -> fine grained control over how to refer objects in other namespaces calling refer with a ns symbol lets you refer to the corresponding namespace’s symbols without having to use fully-qualified symbols
- filters -> :only, :exclude, :rename
- alias lets you call functions in other namespaces with a shorter name (or more semantically relevant)
Project Organization
- source code is in src/
- Dashes in namespace names correspond to underscores in the filesystem
- To use code from a different file, we have to require it
- require takes a symbol designating a namespace and ensures that the namespace exists and is ready to be used.
- require lets you alias a namespace when you require it using :as or :alias
(require '[the-divine-cheese-code.visualization.svg :as svg])
- equivalent to require and then alias
ns macro allows you to incorporate require, use, in-ns alias and refer ns refers the clojure.core namespace by default
(ns the-divine-cheese-code.core
(:refer-clojure :exclude [println]))
Chapter 7
- Macros allow you to transform arbitrary expressions into valid Clojure
Clojure Evaluation model -
- Reads textual source code first -> produces Clojure data structures
- Clojure data structures -> evaluated
Homoiconic -> reason about the code as a set of data structures that you can manipulate programmatically.
-
Lisps evaluate native data structures
-
Clojure still evaluates tree structures, but the trees are structured using Clojure liss and the nodes are Clojure values
-
Clojure reader converts code (+ 1 (* 6 7)) into a nested list.
-
Clojure evaluator takes that data as input and produces a result. (also compiles to the JVM bytecode)
Takeaways
Coming back to problems after a while is good and can yield productive results. I also need to revisit old functions and what I’ve learned previously to keep the knowledge fresh in my mind.
Today’s tally -
- 1 4Clojure problem
- 1.5 Brave and True Clojure chapters