As Clojure programmers, our software is full of trees. Both the values we manipulate and the code we manipulate them with, are made from trees. It's all to easy to get caught up in a project and miss the forest for the trees, but how often have you missed the trees for the forest? This presentation is an exploration of trees from a unique perspective that will hopefully inform your thinking and lead your program design out of the woods.
3. once upon a time in #clojure…
bbloom: better ways to analyze and process
trees is a bit of a new obsession of mine :-)
gfredericks: clojurebot: bbloom is a dendrologist
clojurebot: Alles klar
48. Symbols: The Original Identities
(def x "top-level")!
cljs.user.x = "top-level";!
!
!
(fn []! v a r
x!
(let [x "local"]!
x))!
function () {!
cljs.user.x;!
var x = "local";!
return x;!
}
loc al
b o l re s o l v e d
sym
g c o n te x t !
us i n
49. Context: Not Just For Identities
(def x "top-level")!
cljs.user.x = "top-level";!
!
!
(fn []! s t a te me n t
x!
(let [x "local"]!
x))!
t ai l p o si t io
n
function () {!
cljs.user.x;!
var x = "local";!
return x;!
}
x t u a l re t u r n !
c o n te
50. A
B
E
C
F
D
G
(def vector-tree!
["A" ["B" ["E"] ["F"]] ["C"] ["D" ["G"]]])
(def tree!
{:label "A"!
:children [{:label "B"!
:children [{:label "E"}!
{:label "F"}]}!
{:label "C"}!
{:label "D"!
:children [{:label "G"}]}]})
51. A
0!
!
1!
B
C
D
!
2
!
E
F
G
(defn annotate-depth [node]!
! (letfn [(f [node depth]!
!
(let [d (inc depth)!
!
annotate-child #(f % d)]!
!
(-> node!
!
(assoc :depth depth)!
!
(update-in!
!
[:children]!
!
(mapv annotate-child %)))))]!
(f node 0)))
52. 2
2
2
!
E
2
1
B
F
A
C
2
D
2
G
(defn annotate-max-depth [node]!
! (let [{:keys [children]} node]!
!
(if (seq children)!
!
(let [children*!
!
(mapv annotate-max-depth children)]!
!
(assoc node!
!
:max-depth (apply max!
!
(map :max-depth children*))!
!
:children children*))!
!
(assoc node :max-depth (:depth node)))))