Closure compiler externs files for underscore and backbone js
by Luke Rodgers on May 8, 2012
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.
Errors in scripts loaded with jQuery $.getScript
by Luke Rodgers on April 20, 2012
Spent some time the other day trying to figure out why I wasn’t seeing JavaScript errors in certain files where I expected to, which was problematic both for development and also for the monitoring of client-side errors we’re doing in the application (assigning a handler to window.onerror that makes an ajax call with error information to an endpoint for this purpose).
Basically, if there are errors in any of the scripts you are loading with $.getScript, or the full $.ajax call for which $.getScript is shorthand, these will be caught by jQuery and will fail to show up in the console or be passed to any window.onerror handler you’ve defined. jQuery provides a global ajaxHandler you can attach to, for example, document, which you can then use to do whatever client-side error handling/logging you want to do.
The downside of this approach is (as far as I can tell) that you don’t get quite as much error information (specifically, the line number of the error) from jQuery as you do from other ways of loading scripts with js, because the error gets caught during a call to eval (or more specifically, jQuery’s globalEval method).
JScript deviations from ES3
by Luke Rodgers on April 13, 2012
Came across this excellent resource the other day when looking into some failures for ie8 in the test suite for es5-shim. If you spend any time trying to understand and keep track of differences in ecmascript implementations, especially in older IE browsers, this will probably be of great interest to you.
Objects in closure compiler record types
by Luke Rodgers on April 11, 2012
I’ve been diving much deeper into closure compiler’s type checking abilities lately, and while it is a great tool, the documentation could stand to be improved somewhat.
Sometimes when creating a record type, you will want to specify not just that some property is an array, or an object, but what sort of things are in the array, or what sort of properties are on the object.
A simple record type for an object with a string and number property:
/**
* @typedef {{name: string, index: number}}
*/
A record type with an array of strings:
/**
* @typedef {{networks: Array.<string>}}
*/
A record type, one of whose properties is an object:
/**
* @typedef {{authorized:boolean, network_name: string,
* details: Object.<{id: number, promotion_id: number}>, type: string}}
*/
This last one is the syntax I was having trouble coming up with, and it is not immediately obvious from the official documentation here. Hope this might help someone else who’s looking for the same thing.
Automatic brain, free person
by Luke Rodgers on February 12, 2012
I agree with much of what Michael Gazzaniga says here, and with the overall thrust of the project exemplified most recently in his book Who’s in Charge?: Free Will and the Science of the Brain.
However, as much as we might like to make this project entirely palatable, there is an important sense in which it does seem to challenge some stubborn intuitions, and I think we need to recognize the fact that such findings can and do influence our ideas about how and when to hold people responsible. The clearest evidence for this is found in the legal realm where, from the insanity defence to the twinky defence, we encounter a long history of attempts to grapple with the relation between the apparently mechanistic nature of the brain and our ability to hold people responsible for their actions–attempts which clearly show that a scientific-mechanistic understanding of the brain has important bearing on our understanding of freedom and agency.
The Hood Internet – One Midnight With You
by Luke Rodgers on January 10, 2012
I suppose eventually I will get tired of M83 mashups, but it hasn’t happened yet.
The Hood Internet – One Midnight With You (Mayer Hawthorne x M83) by hoodinternet
Project Euler problem 9 in one line of Haskell
by Luke Rodgers on December 28, 2011
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.








