Blake (old posts, page 8)

Why is this stuff so hard?

A couple of days ago, I fixed a bug by deleting the class. I don’t often get to do that, but this class was buggy, hard to use, and ill-considered in the first place. I won’t post the code, but some sample output is:

Fri Aug 27 00:00:00 EDT 1976
Julian Day: 2443017
Milliseconds since midnight: 43200000
Thu Aug 26 20:00:00 EDT 1976
Clearly that’s not right, since the top and bottom date should be the same. So, what was wrong with it? Well...
  • Julian dates use milliseconds since noon, not milliseconds since midnight.
  • The milliseconds since noon should be 57600000, because we’re in EDT, which is 4 hours behind UTC. (The cause of this particular bug.)
  • The class was implemented as a subclass of java.util.GregorianCalendar. A Calendar exists soley to link a java.util.Date and a java.util.TimeZone, but Julian dates are all relative to UTC, so should not be represented as a Calendar, much less a Gregorian Calendar.

I managed to replace almost all the occurrances with java.util.Dates, except for a couple of places where I needed to group things by day in EST. Those places I just created a new java.util.GregorianCalendar, and used it to figure out where to split my groups. The algorithm to go from the Julian days and milliseconds since noon to a java.util.Date, and back again is fairly easily derivable from the formula:

date = (julianDate - JULIAN_START) * MILLIS_PER_DAY + julianMillis - MILLIS_PER_DAY/2
Solve for any one of the three lowercase variables, using a little truncating-division, or a little modulo arithmatic to get rid of unknown terms, and you’re good to go. So please, if you’re dealing with dates or times, don’t make this mistake, at least not where I might run across it.

So that’s why 34-year olds don’t skateboard.

A friend of mine (or more accurately, his parents) recently offered us an air-conditioner. A most welcome addition, given the silly heat levels we’ve been experiencing, and the complete lack of any cooling facilities in our house. But that left me with the problem of how to get it from their house to ours. I could probably have carried it, although it would have been really tough. I might have been able to borrow a bike trailer from someone (or fit it into the kid’s bike trailer (more on that in an upcoming post), but I didn't really know who, or where, and I kind of wanted to get it over here and hooked up fairly quickly. Fortunately, I remembered that I had seen a skateboard amongst some old garbage a block away from my house, so I walked over, and grabbed it.

Now, in my younger days, I used to be quite the ‘boarder. Okay, I was never really that good at getting air, but I had no problems getting around on it. So I thought I’ld give it another try. The first push went well, and I glided along for a couple of meters, before hopping off, feeling a little silly. I walked to the end of my street, and then thought about how cool it would look to cruise up to the front door on the skateboard, so I put it down, and hopped on. But this time, my front foot was facing forwards instead of sideways, and when I tried to turn it (in Birkenstocks!), it sort of didn’t, and back I went, landing flat on my ass, elbows, and shoulderblades. I’m fairly sure I didn’t hit the back of my head, at least not too hard, and it was more my pride that was damaged than anything else, although the muscles on the front of my neck are really rather sore. It did, however, answer one important question. I’ve still got “it”, it’s just been packed away and put in the basement, perhaps to be brought out and given to my children in a couple of years.

P.S. It's even scabbier now!

Down With Centering!

Recently, I've been reading a few books by Robin Williams (the author, not the actor) about designing for print, and for the web. One of the things she mentions is that centering looks a little bland, corporate, boring, and you should instead attempt to align things to the left or the right. So I took a few minutes tonight to attempt to do that with the blog, aligning the stories on the right, mirroring the strong line of the sidebar, which has changed to left-alignment. I also changed the boxes to lines on the aligned side. I'ld love to hear what you think, if you noticed at all. (I'm leaving the colour scheme to Amy. A large part of design is knowing your limitations.)

Does the Future Belong to Javascript?

J

This started off as a response to Greg's post, but grew too long, so I figured I'ld put it here instead.

The reaction I've seen from the developer community to Apple's "iPhone Development" announcement has been poor at best. Everyone seems to think that it's a cop-out, and that they should have just said "We don't support third-party development, but you can use the web." My favourite quote has to be from John Gruber:

If all you have to offer is a shit sandwich, just say it. Don't tell us how lucky we are and that it's going to taste delicious.
Maybe it's because I'm an old fuddy-duddy, but I don't think the Web 2.0 development is really the wave of the future. I think the future, like the past, will have desktop apps, client-server apps with both thick and thin clients, where the server may be a web server or may not, etc, etc...

And Javascript is still missing the extreme number of useful libraries that the other three languages have. Like Calvin Spealman says:

Does syntax alone make a language? Maybe one day it could, but those days died out. Python is far more than its clean, beautiful syntax. The libraries that come in the standard library provide even more value. As a foundation for all the software built on top, these packages are fundamental to the success of Python.
and I find that to be true over and over again. Java the language kind of sucks. But the huge number of libraries to do just about anything I want really rocks. (And the IDEs are sweet too, which is a good thing, since trying to write Java without an IDE would give even a super-robot a serious case of carpal tunnel syndrome.)

Which is not to say that there won't be more and more libraries shipped with Javascript, but then again, since it's mainly a web-language, the browser makers will want to keep it light, and so the libraries won't be part of the language proper, as they are with Python or Java, and any "standard" library you see will be vendor-specific, just like it is today. (I already write some Javascript scripts for my day job, but I know that they're going to run on a Windows box. I have no idea how to write the cross-platform equivalent of stuff like:

if ( FS.FileExists( flavour + "\\gamelist.txt" ) )
or:
WScript.Echo( "Unhandled folder: " + fname );
much less:
var WSHShell = WScript.CreateObject("WScript.Shell");
var cmdline = "jar.exe xvf " + item + " META-INF/MANIFEST.MF";
WSHShell.Run( cmdline, ActivateAndHide, true );
Any suggestions?

And while I'm here, that red "J" was the best thing I could come up with for a Javascript logo, after extensive web searching. (There's a reason I'm a programmer, and not a graphics person.) Anyone seen anything better?

Well, That Was Unexpected.

Since it was such a nice day today, I rode home a little slower than I otherwise would, and it was really nice. I talked to a woman biking along holding on to another bicycle beside her (apparently it's not as hard as it looks), and a man who took a shorter route to Summerhill and MacLennen than I do. People seem really nice to each other when they're on bicycles...

While I was cycling east along Gerrard, I noticed several police motorcycles blocking the bike lane. The cops were more than happy to wave me through, but does anyone know why there were there?

And finally, the interesting thing. On Merton, I was biking fairly closely behind a car (but slowly, because there are a lot of speed bumps), when they hit the brakes and turned right into a parking spot. I grabbed my front brake hard to slow down, and it went *ping* and offered no resistance. So I hit my back brake, and slowed right down, but apparently when I grabbed my front brake I snapped the cable! First time I've ever done that, and it was kind of exciting in a "I don't really want to do that again" kind of way. I got to the local bike shop (Sport Swap, now just south of Bayview and Davisville! Sadly the new location didn't make it into the 2007 Bike Map, which lists the old location (now a Trek Store), so I figured I'ld throw the info here, since they've been really nice to me.) Uh, anyways, where was I? Oh, yeah, I got to the local bike shop just after they closed, so I couldn't get it fixed tonight, but fortunately, there's a breakfast at The Bike Joint (290A Harbord St.) tomorrow, which I think I can make it to using only my rear brake, and I'm sure they'll be able to get me rolling again.

Bike Week 2007.

Well, Bike Week 2007 has kicked off, and contrary to the first two years I took part, this year I actually got a pancake! Woo! My secret was sleeping in, so that I couldn't make it up to Yonge and Lawrence by 7:30, and instead heading straight to Yonge and Bloor for 8:00. On the downside, they didn't have any t-shirts larger than a medium, so I guess I'll need to lose a little weight before wearing the one I got. On the upside, I was totally at the front of the pack, and had a perfect view of Mayor Miller catching his front tire in something and going head-first over his handlebars! Very exciting, and he seemed to be fine, although he did have to trade his fancy new road bike in for an older mountain bike to complete the ride.

What else... It was a nice ride in, the pancakes and croissant were pretty much as I expected them to be. The coffee was pretty sweet, though. I also picked up an apple and a bottle of water, but I haven't had them yet, so I don't know how they are. (The apple is a really nice dark shade of red, though, so I'm looking forward to it.) This year's t-shirt is much prettier than the previous two years, I feel. I got a couple of extra bike maps, and have distributed them among the people in my office who cycle. Uh, I guess that's about it.

Oh, and I need to get the new Bike Week logo in svg somehow... I would use the other bike logo I've got, but it's used in the post right below this one, and that seems kind of repetitive.

Thoughts from a cyclist in the rain.

After a certain point, you don't really get any wetter.
Even though it's really cold, and I'm totally soaking, it's not a bad ride.
Wow, it takes a long time to stop. (I already knew this, and I'm going way slower than I normally would, but that doesn't make it any less true.)
I could probably do with a new pair of biking gloves.
The headband I stole from Amy is working wonderfully, though.
I hereby transfer all the karma I got from giving my tires to Bike Pirates to the nice person in the car (a Beemer, even!) who let me turn left into the cemetary front of them.
Although, thinking about it, I might not have that much karma left, since I'm biking on the sidewalk. (Up Mount Pleasant beside the Cemetary. In this weather, there's no way I would feel safe on the road, and there aren't any cross roads or pedestrians, so I'm probably way safer than I would normally be on the sidewalk.) Mr. Walker, a bike lane down Mount Pleasant would be most appreciated.

Yearrrghhh!

So, in an attempt to fix Katherine's bug, I've managed to move all the entries to a couple of days ago. I moved back the ones that were in my Bloglines cache, but I don't have the correct dates for prior ones, so I'm kind of hosed until I look in my backups. Unless any of you happen to have them in your caches, of course...

Also, yargh.

Isn't that just the way of it...

I mentioned in the previous entry that I was missing Python's List Comprehensions. Well, I've gotten a little closer to having them. For some reason, LispMe doesn't come with a zip method, and you can only get map by importing a "Standard Library" memo. So, I had to write my own version of zip, and here it is, for anyone else who might find it useful.

1
2
3
4
(define (zip s1 s2)
  (if (or (null? s1) (null? s2)) '()
    (cons (list (car s1) (car s2))
      (z (cdr s1) (cdr s2)))))

Along with my halfway function, redefined to be (define (halfway x) (/ (+ (car x) (cadr x)) 2)), I can now write (map halfway (zip current-point next-point)) to get the point halfway between where I am, and where I am going to.

I also came to another realization. I was planning on defining a current-point, and using set! to update it to the new halfway point, but when I think about it, I don't really care what the current point is at any time other than processing, so there's no particular need to set it, and instead I should just pass it around as a parameter, making my iter function (and I just made up the name "iter function", based on the <a href= "http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html#%_sec_1.1.7"

sqrt-iter function in "Structure and Interpretation of Computer Programs") look something like this:

1
2
3
4
5
6
(define (triangle-iter start num-iters)
  (if (positive? num-iters)
    (begin (draw-point start)
    (triangle-iter
      (map halfway (zip current-point next-point))
      (- num-iters 1)))))

Does that look appropriately Scheme-ish, do you think?

Starting Scheme.

(Posted to the LispMe@YahooGroups.com list) Hello.

Let me start with a brief introduction. My name is Blake Winton, and I’ve owned a PalmPilot since back when they were called PalmPilots and made by USRobotics. I’ve been programming for them for years, mostly in C and C++, with brief excursions into Lua, Python, and Forth. My day job consists mainly of Java (J2EE), with some Python when I get the chance, and some JavaScript when I shouldn’t be using Python. On my off hours, I read a lot about other languages (recently Objective C, OCaml, and Ruby). Now I want to learn Scheme, and thought that LispMe would be a good way to do it. I’m finding some things are tripping me up, mainly due to my attempts to transfer my knowledge from other areas into LispMe. (One piece I miss in particular are something like Python’s List Comprehensions, which I believe they sole from Haskell.)

So, I’m going through SICP, but while I’m doing that, I thought I would try to do a "Real World" (tm) task, and write a program that drew the Serpinsky Triangle, using an iterative, random, approach, (Details available upon request,) and I’ve run into some small questions about best practices, or basically how to do some simple things. In return for this help, I’ll create a sort of Tutorial document for LispMe that will be able to help other people get up to speed. (If such a document already exists, please, someone, point me to it!)

So, here’s what I’ve got so far. It doesn’t work at all, but it’s starting to take shape.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
; Triangle
(define points
  #((80,10) (10,150) (150,150)))

(define (next-point p) (
  (vector-ref points
    (random (vector-length points)))
  ))

(define current-point '(80 80) )
(define (halfway a b) (/ (+ a b) 2))

And we’re done. Any comments on it, from spacing to indentation to whether I should use a vector or a list for the points themselves, would be greatly appreciated. If it matters, I plan on extending the point to include red, green, and blue data as well, possibly with accessors, looking something like this:

1
2
3
4
5
6
(define current-point '(80 80 255 0 0))
(define (x pt) (car pt))
(define (y pt) (cadr pt))
(define (r pt) (caddr pt))
(define (g pt) (cadddr pt))
(define (b pt) (caddddr pt))

Oh, and I suspect I’ll be blogging my progress at http://weblog.latte.ca/blake/tech/scheme/ (Nothing exists there at the moment, though. Give it time.)

Thanks,
Blake.