Avoiding 503s when loading images from Google Contacts API

There’s a variety of mildly annoying things about V3 of Google’s Contacts API (lack of control over which fields are returned, basically non-existent sorting ability, use of oauth bearer tokens; also they seem to be confused about their supposed mitigation of the confused deputy problem: validation does not actually seem to be required, nor does this in any way prevent an attacker from stealing the token and doing whatever they like with it).

One of the most annoying “features” is the undocumented rate limit when requesting images for a user’s contacts, each request of which must be authenticated. The rate limit appears to be around 10/s.

I wrote a lightweight jQuery plugin that will help you avoid the 503 errors that Google’s API returns when you begin exceeding this limit.

jQuery batched image loader

The github page has basic implementation details.

Scrolling while paused in Chrome debugger

If you’ve spent much time with Chrome’s debugger, you’ve probably encountered a few annoying scenarios, in which the locked-up view of the web page you’re working on prevents you from inspecting elements or scrolling.

An easy workaround for the scrolling lock-up is to just jump to the console, and:

window.scrollTo(0, 800)

Backbone.js finite state machine view

I developed a basic Backbone.js finite state machine (FSM) view for use on a project I was working on a while ago, and have been meaning to clean up the code and share it for a while now. It is now available on github, along with some qunit and sinon tests.

It was written to handle complex form navigation, i.e. when you want to conditionally guide a user through a path of form fields depending on their previous answers, and so while it could perhaps be shoehorned into other uses, this is where it will be the most natural fit. It works basically by binding a method to the change event on form fields that translates this event into terms understood by the FSM, and then calls transitions that you’ve defined when initializing the view.

There is some basic js and HTML on the github page demonstrating use. I plan to have more/better code, along with a working example, at some point.

backbone-fsm-view

Since writing this a few other people have come up with implementations that, though they appear quite different from mine, would accomplish mostly the same thing. See backbone.statemachine, and machina.js. Also, I definitely got inspiration for this code from a few other places on the web while writing it, but can’t track them down now. If anyone reading this thinks it reminds them of something they’ve seen elsewhere, or if they have suggestions on how it could be improved, feel free to let me know. I’m always open to suggestions and/or pull requests.

Closure compiler externs files for underscore and backbone js

As part of some other things I’m working on I’ve begun building upon some existing closure compiler externs files for the Underscore and Backbone js libraries. Both are still in quite early stages, but if you need something like this, they’re currently better than nothing.

backbone-js-externs on github, for Backbone 0.9.2

underscore-js-externs on github, for Underscore 1.3.3

The Backbone externs file is taken from this gist, which was autogenerated by DotNetWise’s externs extractor. It does a decent but very basic job.

The Underscore externs file is taken from the official Closure Compiler repository. The only changes I’ve made so far are

  • fixing a type annotation for the _.uniq and _.unique functions that had a required parameter after a non-required one (Closure Compiler dislikes this)
  • using same type annotation for _.uniq and _.unique, since the latter is an alias for the former
  • beginning to add type annotations for functional-style usage of underscore, vs. object notation (e.g. _(collection).map(function(){}) vs. _.map(collection, function() {}). The existing externs file has only the latter.

I’ll probably also be adding externs for use with the SVG library Raphael at some point.

Project Euler problem 9 in one line of Haskell

Problem 9 of Project Euler asks you to find the product of the sole Pythagorean triplet, the sum of whose values is 1000, where a Pythagorean triplet (familiar from elementary school math) is a set of natural numbers  a < b < c where a^2 + b^2 = c^2.

This snippet of Haskell solves it in one line and demonstrates some of what makes Haskell an interesting language, but takes several minutes to run on my MBP:

problem9 = take 1 [a * b * c | a <- [1..1000], b <- [1..1000], c <- [1..1000], a + b + c == 1000, a^2 + b^2 == c^2]

A version with some simple improvements runs much faster. Since a < b < c, we know the largest possible value for a is 332 (since 333 + 334 + 335 is 1002). And since b must be larger than a, we can narrow down its possible values as well, based on a. C does not need to be independently calculated at all, since by definition it must be a value that will satisfy the Pythagorean theorem. So:

problem9' = floor $ head [a * b * sqrt (a^2 + b^2) | a <- [1..332], b <- [(a+1)..(999-a)], a + b + sqrt (a^2 + b^2) == 1000]

This version runs in a couple seconds.