Nerdworks logo "The nerd shall inherit the earth."

Nerdworks Blogorama

Nerdspeak

IE9 web cast / Chennai Web Camps / OSI Days 2011
Technobabble
11/24/2011 4:06:30 AM  

It has been a somewhat hectic two weeks shuttling between Chennai and Bangalore for a bunch of stuff. As usual, I’ve been happily handing out promises about posting resources for my talks on my blog. Like candy. Turns out you’ve got to come good on them eventually. So here goes:

IE9 Web Cast on ECMAScript 5

  1. Get the deck (there isn’t much in the deck but there it is for what its worth).
  2. Get the JavaScript eval console I was showing off in the session.

Chennai Web Camps

All the resources for the web camps sessions are available here.

OSI Days 2011

  1. Session on WebMatrix: Building a complete website within an hour
  2. Session on building cross browser HTML5 web apps

That’s all folks. Hope the sessions were useful. If you’ve got feedback on any of these, please feel free to leave a comment. If you want to be nasty and use dirty words, please email them to me.

Link Comment
 
Partial function application in JavaScript
Technobabble
11/7/2011 9:32:22 AM  

Partial function application is a functional programming technique that allows you to transform functions that take N parameters into functions that take a subset of N parameters. For example, say you have this fairly useless function:

function add(a, b) {
    return a + b;
}

Let’s say that for some reason we need a version of "add" that will always have the value 5 for the parameter "a". One way we might do it is like so:

function add5(b) {
    return add(5, b);
}

No black magic there. I was wondering if there might be some way that we can automate the process of generating functions with part of their arguments pre-bound. Here’s what I came up with:

function partialBind(f, context) {
    // arguments length MUST be greater than 2
    if(arguments.length <= 2) {
        // coz. what're you "partial binding" otherwise?!
        return null;
    }

    // convert args to array excluding param "f" and "context"
    var arrayProto = Array.prototype;
    var params = arrayProto.slice.call(arguments, 2, arguments.length);
    return function () {
        // append params passed to this function to "params"
        var args = params.slice(0, params.length);
        if (arguments.length > 0) {
            arrayProto.forEach.call(arguments, function (p) {
                args.push(p);
            });
        }

        // call the original function
        return f.apply(context, args);
    }
}

This function allows me to transform pretty much any function that takes at least 1 argument into a new function that takes a subset of the arguments. Transforming our "add" function into a single parameter version with "a" bound to 5 for instance, would now look like this:

var add5 = partialBind(add, null, 5);
var val = add5(10);
assert(val === 15);

Interestingly, "partialBind" allows us to compose argument binding to multiple levels. If we wanted to take our "add5" function and create another version that binds the second parameter to, say 20, then we can do this:

var add5_20 = partialBind(add5, null, 20);
var val = add5_20();
assert(val === 25);

Note that we are doing a partial bind of a function that is already a partial bind of another function. Turns out we can take this to as many levels as we want. The example given above is of course somewhat concocted because we could have achieved the same result with a single partial bind instead of doing it in two levels:

var add5_20 = partialBind(add, null, 5, 20);
var val = add5_20();
assert(val === 25);

All of this works because JavaScript supports closures. Pretty nifty don’t you think?

Link Comment (2)
 
Web Camps, Virtual Tech Days and jsFoo
Technobabble
10/17/2011 5:07:14 AM  

So I’ve had a busy few weeks recently doing a bunch of sessions at Web Camps (in Delhi and Hyderabad), Virtual Tech Days and jsFoo. At each of those events I promised attendees that I’d put up the demos and the decks on my blog “soon”. Well, truth be told I’ve not been exactly conscientious about doing this quickly and I apologize for that. At any rate here’s the material for folks who came by for these sessions:

Web Camps

  1. Bold and Beautiful with CSS3deck / demo
  2. Spice it up - Advanced HTML5 – deck / demo

Folks who attended the Hyderabad event might remember that I used a HTML5/CSS3 version of the decks during the session. If you’d like a copy of that code then hang tight because I hope to put up a post here on how I built it and intend to share the source with that post.

Virtual Tech Days

  1. Socket programming on the webdeck / demo
  2. Threading and async programming on the webdeck / demo
  3. Baking some HTML5 goodness into your ASP.NET appsdeck / demo

jsFoo

  1. A primer on ECMAScript 5deck
  2. Advanced JavaScript Techniquesdeck
  3. Best practices for building async apps with JavaScriptdeck / demo

A number of jsFoo attendees requested that I share the “eval” console that I used to geek out on many of the advanced JS and ES5 samples during the session. I already have a post on that here:

http://bit.ly/js-eval

You should be able to download a copy for your own use from that post or use the online version if you prefer that.

Link Comment
 
blogorama home
about this blog
email the author
where on earth am i?
subscribe to mailing list
feeds Use these links for feed syndication
rss  |  atom
by category
technobabble (54)
philosophical crud (3)
irrelevant stuff (7)
archive
september, 2011 (7)
july, 2011 (3)
june, 2011 (2)
may, 2011 (3)
april, 2011 (1)
march, 2011 (1)
february, 2011 (1)
february, 2010 (1)
october, 2009 (1)
september, 2009 (1)
july, 2009 (5)
march, 2009 (2)
august, 2008 (2)
march, 2008 (1)
january, 2008 (1)
september, 2007 (2)
april, 2007 (1)
february, 2007 (2)
december, 2006 (1)
october, 2006 (1)
september, 2006 (4)
august, 2006 (3)
july, 2006 (4)
june, 2006 (3)
may, 2006 (6)
april, 2006 (2)
recent entries
IE9 web cast / Chen...
Partial function ap...
Web Camps, Virtual...
176872 hits