Archive for December, 2008

New Year’s Probability Fun!

December 31st, 2008

There’s a post today over at coding horror that proposed a very simple probability problem. After more than 400 comments (and counting) I’m very surprised that so many people have the wrong answer. The question is this:

Let’s say, hypothetically speaking, you met someone who told you they had two children, and one of them is a girl. What are the odds that person has a boy and a girl?

Actually, I’m not sure if by “odds” Jeff meant this, or probability — as is the case in most everyday use. But assuming that he meant probability, then the solution is as follows.

Let A be the event of having a girl.
Let B be the event of having a boy.

Assume that P(A) = P(B) = 0.5

We know that the person has a girl, thus we are trying to find P(B|A) (probability of B given that A has occurred).

Now we note that A and B are independent events, thus P(B|A) = P(B) = 0.5 (conditional probability).

And there you go, the probability of the person having a boy given that person has a girl is 0.5 (or 50%).

Happy New Year’s folks!

Do you mean partial application?

December 8th, 2008

As I’ve been digging through more functional programming techniques, I often see people mention Curry functions. As it turns out, when a lot of people say “curry” they really mean Partial Application. For example, this page here about curry implementation in Python, or implementations in JavaScript. I did find one implementation in JavaScript that worked, but it’s rather long.

I came up with my own implementation in less than 10 lines of JavaScript code. Before I show it though, let’s define what we should expect. The follow are the JavaScript code we’re going to run, and the expected output are in the comments.

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

function add5(a, b, c, d, e) {
    return a + b + c + d + e;
}

function saySomething(x, y, z) {
    alert(x + y + z);
}

add.curry()(4)(6);
//--> 10

add5.curry()(1)(2)(3)(4)(5);
//--> 15

add5.curry()(1)(2);
//--> function with one parameter

add5.curry()(1)(2)(3)(4)(5)(6);
//--> TypeError: add5.curry()(1)(2)(3)(4)(5) is not a function

saySomething.curry()('Hello ')('World! ')('foo bar');
//--> Alert mssage "Hello World! foo bar" pops up

Remember that currying a function, say f:(X x Y) –> Z means that curry(f):X –> (Y –> Z). And for a function g with n arguments, curry(g) transforms the function g into a chain of n functions that each take exactly one argument.

So here is my implementation of the curry function.

Function.prototype.curry = function() {
    var savedArgs = [],  n = this.length, func = this;

    return (function(x) {
        savedArgs.push(x);
        if (savedArgs.length < n) return arguments.callee;
        else  return func.apply(null, savedArgs);
    });
};

Note that is utilizes anonymous functions in JavaScript, which recursively calls itself using the arguments.callee reference.

Publisher/Subscriber Fun with jQuery

December 5th, 2008

I’ve decided to try out the Publish/Subscribe pattern for some of the new things we’re creating at work. So far I think it’s working out really well. The neatest thing is that a Pub/Sub system can be created in JavaScript in very little amount of code. And with the help of jQuery, you can shorten the amount of code even more.

Here is the entire system in jQuery.

jQuery.subscribe = function(signal, scope, fnName) {
    var curryArgs = Array.prototype.slice.call(arguments, 3);
    $(window).bind(signal, function () {
        var normalizedArgs = Array.prototype.slice.call(arguments, 1);
        scope[fnName].apply(signal, curryArgs.concat(normalizedArgs));
    });
    return jQuery;
};

jQuery.publish = function(signal) {
    $(window).trigger(signal, Array.prototype.slice.call(arguments, 1));
    return jQuery;
};

Notice that the subscriber function allows for partial applicationof the handler function. This can be useful in some cases.