sunnuntai 13. syyskuuta 2009

C++ Kana #2 — Dependency Injection

This time we'll exercise templates and the typeid keyword to build a Guice-inspired dependency injection framework. Write class Injector that allows you to write


class IService {
public:
    virtual ~IService() {};

    virtual void serve() = 0;
};

class HelloService : public IService {
public:
    void serve() {
        std::cout << "Hello, world!" << std::endl;
    }
};

int main() {
    Injector injector;

    injector.bind<IService>().to<HelloService>();

    IService* service = injector.newInstance<IService>();
    service->serve();
    delete service;

    return 0;
}

C++ Kana #1 — Usable Logging

Let's exercise RAII and streaming. Write function line() that allows you to write

line() << "hello world " << 5 << "!";

transforming to

std::cout << "hello world 5!" << std::endl;

maanantai 20. huhtikuuta 2009

Arguing on Taste

A saying goes: "you can't argue with taste". This is neither true nor false: the saying is too ambiguous to warrant such classification.

This is a classical case of equivocation—the confusion of homonyms with synonyms. The problem is that the word "taste" has two distinct meanings:

1) The subjective response to a stimuli. These cannot be argued.

2) The ability to discern the objective merits (artistic, aesthetic, culinary, etc.) of a work. These can be argued.

Lest this sounds too cold-hearted and ridding of emotions, it should be stressed that "having good taste" (of the second kind) may also suggest a positive correlation between one's subjective response and objective merits.

maanantai 16. helmikuuta 2009

Probabilistic Modus Tollens

Here's the rule that justifies the classical modus tollens inference:

[A => B] <=> [~B => ~A].

An example: if all kings have crowns, then not having a crown means one is not a king.

However, in real world, things aren't black-and-white. Therefore, a probabilistic equivalent of modus tollens would be nice. Here is one:

[P(A | B) >= P(A)] <=> [P(~B | ~A) >= P(~B)].

An example: if kings have crowns more often than other people, then not having a crown decreases (or doesn't increase) the probability of one being a king.

Proof is left as an exercise. (Hint: apply Bayes's rule and the rule of complementary probability.)