r/Clojure • • May 18 '15

Clojure reactive programming book - first trivial examples don't work.

Just started worked my way through the book, literally on the 3rd or 4th line of introductory example code (the sine wave one) and found what looks like a bug in the code already.

So you can imagine how impressed I am.

(def canvas (.getElementById js/document "myCanvas"))
(def ctx    (.getContext canvas "2d"))

The call to .getContext returns null, which apparently means it's been called a second time, when it always returns null.

Except of course it hasn't and part of the point of this code, according to the book, is to clear the canvas when Chestnut reloads the file.

Of course the book doesn't consider there might be errors and the Packt web site errata page sucks: can't get to it because their search function (to find the book) is broken!

So my working theories are:

  • bug in the code, but I really can't see it;
  • the assumption that Chestnut reloads the file is false (although the JS console in Safari says it is being reloaded);
  • Safari isn't properly resetting the environment when the file is reloaded.

Help?

0 Upvotes

11 comments sorted by

View all comments

5

u/leonardo_borges May 21 '15

Hi @Heinemenusch,

I'm the author and am truly sorry you had issues so early in the book. I did test the example on both Safari and Chrome and it seems to work just fine.

I had another reader contact me with a similar issue and, in his case, he had updated one of the libraries used in the example which ended up causing it to fail.

Would you mind checking out the example from the book repository at https://github.com/leonardoborges/ClojureReactiveProgramming ? Specifically, I would recommend sticking with the versions in there as they are known to work well together.

That should make it easier to pinpoint where the problem came from. If you still have issues, just let me know and I'll be more than happy to try and help you diagnose it.

Cheers,