Archive for October, 2012
coffeescript fun
I just wrote probably the most ridiculous, and sort of awesome, code of my life to date. 10 points if you can figure out what it does and what the point of it is (and 20 points if you find a bug!)
The code exports two functions, u.bind and u.let (which is syntactic sugar for a special case of u.bind). Here’s an example / test-case of using them:
class Thing constructor: -> @x = 0 setInterval => @x += 1 @get_x_multiplied_by.update_all() , 1000 get_x_multiplied_by: u.bind (y) -> @x * y a = new Thing() u.let -> document.title = a.get_x_multiplied_by 7
And here’s the code that defines u.bind and u.let:
#u = module.exports u = {} #for in-browser testing u.UUID = -> (Date.now()) + 'x' + Math.round(Math.random() *1e18) _running_bound = null magic_number = u.UUID() smish = (thing) -> if thing != null and (typeof thing) == 'object' if not thing[magic_number] thing[magic_number] = u.UUID() return thing[magic_number] else return (typeof thing) + thing smush = (args...) -> return (smish arg for arg in args).join('') run_bound = (bound, fn, args) -> hash_key = smush args..., this if _running_bound if not bound.depends_on_me[hash_key] bound.depends_on_me[hash_key] = [] if _running_bound not in bound.depends_on_me[hash_key] bound.depends_on_me[hash_key].push _running_bound old_running_bound = _running_bound _running_bound = {bound: bound, args: args, old_this: this} if bound.results_cache[hash_key] == undefined bound.results_cache[hash_key] = fn.apply this, args bound.args_cache[hash_key] = {args: args, old_this: this} _running_bound = old_running_bound return bound.results_cache[hash_key] u.bind = (fn) -> bound = (args...) -> val = run_bound.call this, bound, fn, args return val bound.depends_on_me = {} bound.results_cache = {} bound.args_cache = {} bound.update = (args...) -> hash_key = smush args..., this delete bound.results_cache[hash_key] delete bound.args_cache[hash_key] bound.apply this, args depends = bound.depends_on_me[hash_key] ? [] bound.depends_on_me[hash_key] = [] for depend in depends depend.bound.update.apply depend.old_this, depend.args bound.update_all = -> for key, {args, old_this} of bound.args_cache bound.update.apply old_this, args return bound u.let = (fn) -> bound = u.bind fn bound()
If you paste the code followed by the test case in the “try coffeescript” window of coffeescript.org, you can see it in action (watch the webpage title).
(Murakami == Scott Pilgrim)?
Murakami is another one of my favorite authors (especially Norwegian Wood and The Wind-Up Bird Chronicle).
So I have to post this passage from a great essay on his work:
Some have found Murakami’s deployment of fantastical elements in his fiction to be fey or under-justified. His own reasoning about the practice, in a 2004 Paris Review interview with John Wray, is revealing: “We are living in a fake world; we are watching fake evening news. We are fighting a fake war. Our government is fake. But we find reality in this fake world. So our stories are the same; we are walking through fake scenes, but ourselves, as we walk through these scenes, are real. The situation is real, in the sense that it’s a commitment, it’s a true relationship.†So, too, in Murakami’s novels, events might be unnatural and outré, but the characters are as human as possible. Murakami achieves this in two ways: first, by an unrushed, tender cataloguing of small daily action (preparing “steaming foodâ€), and second, by the lovingly humorous imagining of his characters’ inner chatter. Here is Aomame, in a moment of downtime: “That was the most she could get herself to do — stare at the ceiling. Not that the ceiling had anything of interest about it. But she couldn’t complain. Ceilings weren’t put on rooms to amuse people.â€
Compare to my post from a month ago on Scott Pilgrim:
The fascinating thing about the graphic novels, though, is that while the formal elements are completely artificial, the content — i.e., the characters and their inner journeys — feels real. Scott and Ramona awkwardly date, they fall in love, fight, split, and get back together as they learn to be a couple, while at the same time their friends have their own challenges and breakthroughs. Mixed in with the surreal plot points are ordinary slice-of-life scenes where the characters do things like grabbing burgers and building bonfires on the beach.