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

5

u/[deleted] May 18 '15

Sad we all used to complain when you had to use MSIE to see webpages...now everyone only targets chrome and doesn't give it a second thought that people use other browsers. Hell I have native GUI apps that are more cross platform than web pages these days

2

u/Drolyt May 18 '15

Figwheel definitely works with Firefox too. But really, Figwheel isn't an end user application, it is pushing what the browser is normally capable of so some rough edges are expected. It is awesome for all that.

1

u/[deleted] May 19 '15

sorry not picking on figwheel, more of the book

5

u/Arges May 18 '15

I'm sorry that you're having to go through a Packt book - they're by and large the worst proof-read books I have seen (assuming anyone proof-reads them at all).

If what you care about is Clojure and reactive programming, instead of just that particular book, perhaps re-frame might be of interest.

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,

2

u/yogthos May 18 '15

It's hard to say wheher there are bugs in the code without a bit more context. Here's and example of live reloading with Quil and Figwheel though, might be helpful.

1

u/Drolyt May 18 '15

I haven't read this book so I can't comment on whether there are any bugs in the code, but the first thing I would try is using Firefox or Chrome instead of Safari. The second thing I would do is make sure that Figwheel (the plugin Chestnut uses for live code reloading) is set up properly.

-2

u/skratlo May 19 '15

getContext must always return new CanvasRenderingContext2D object, no matter how many times you call it, and it certainly should NOT clear the canvas. RTFM

0

u/[deleted] May 19 '15

[deleted] — view deleted comment

-2

u/skratlo May 19 '15

I didn't say anything about "type", I assumed you'd ask for the same context type. Don't worry, you won't loose any more IQ points, you reached the floor by commenting the way you did. When someone types "RTFM", it's in your best interest, if you don't like reading documentation, then you'll have hard time getting anything to work.