What is up with the Joy of Clojure 2nd edition?
Posted
by
kurofune
on Programmers
See other posts from Programmers
or by kurofune
Published on 2014-06-10T08:02:38Z
Indexed on
2014/06/10
9:40 UTC
Read the original article
Hit count: 395
Manning just released the second edition of the beloved Joy of Clojure book, and while I share that love I get the feeling that many of the examples are already outdated. In particular, in the chapter on optimization the recommended type-hinting seems not to be allowed by the compiler. I don't know if this was allowable for older versions of Clojure. For example:
(defn factorial-f [^long original-x]
(loop [x original-x, acc 1]
(if (>= 1 x)
acc
(recur (dec x) (*' x acc)))))
returns:
clojure.lang.Compiler$CompilerException: java.lang.UnsupportedOperationException: Can't type hint a primitive local, compiling:(null:3:1)
Likewise, the chapter on core.logic seems be using an old API and I have to find workarounds for each example to accommodate the recent changes. For example, I had to turn this:
(logic/defrel orbits orbital body)
(logic/fact orbits :mercury :sun)
(logic/fact orbits :venus :sun)
(logic/fact orbits :earth :sun)
(logic/fact orbits :mars :sun)
(logic/fact orbits :jupiter :sun)
(logic/fact orbits :saturn :sun)
(logic/fact orbits :uranus :sun)
(logic/fact orbits :neptune :sun)
(logic/run* [q]
(logic/fresh [orbital body]
(orbits orbital body)
(logic/== q orbital)))
into this, leveraging the pldb lib:
(pldb/db-rel orbits orbital body)
(def facts
(pldb/db
[orbits :mercury :sun]
[orbits :venus :sun]
[orbits :earth :sun]
[orbits :mars :sun]
[orbits :jupiter :sun]
[orbits :saturn :sun]
[orbits :uranus :sun]
[orbits :neptune :sun]))
(pldb/with-db facts
(logic/run* [q]
(logic/fresh [orbital body]
(orbits orbital body)
(logic/== q orbital))))
I am still pulling teeth to get the later examples to work. I am relatively new programming, myself, so I wonder if I am naively looking over something here, or are if these points I'm making legitimate concerns? I really want to get good at this stuff like type-hinting and core.logic, but wanna make sure I am studying up to date materials. Any illuminating facts to help clear up my confusion would be most welcome.
© Programmers or respective owner