What is Megaphone?

What is Megaphone?
The Megaphone project is about enhancing open source chat software. Specifically, the goal is to allow ejabberd to support 1,000,000 simultaneous users. See The Plan page for more details on how I plan to solve this problem. See the About this Blog page for more details on why I created this blog.

Sunday, February 12, 2012

Fun with States

Previously...
  • I encountered a problem with my test data.
  • I resolved some problems with lists and test data.
  • I resolved some problems with dictionaries.

The next problem that I resolved had embarrassingly little to do with the erlang libraries.  With "imperative" languages like Java I am used to doing something like this:

int foo = 1;
foo++; // foo now has the value 2

Whereas with a functional language like erlang, one instead uses something like this:

Foo = 1;
Foo2 = 1 + Foo;

This is because in languages like erlang, you cannot change a variable once it has been set.  Personally, I don't think one should use the term "variable" to describe such an object in erlang, but I digress.

The problem I was running into was that I was not updating the variable that I changed.  Thus I would use something like this:


put_data(State, ConnectionID, Data) ->
    NewState = update_table(State, ConnectionID, Data),
    { Result, NewerState } = remove_waiter(ConnectionID, NewState),
    case Result of
        undefined -> NewerState;
        Waiter -> notify_waiter(Waiter, ConnectionID, State)
    end.

Whereas what I really wanted was something like this:

put_data(State, ConnectionID, Data) ->
    NewState = update_table(State, ConnectionID, Data),
    { Result, NewerState } = remove_waiter(ConnectionID, NewState),
    case Result of
        undefined -> NewerState;
        Waiter -> notify_waiter(Waiter, ConnectionID, NewerState)
    end.

Nothing really Earth shattering here, just ye basic functional vs. imperative programming styles.  This will not stop me from complaining, but I'm just saying...

Next time: more testing goodness.

No comments:

Post a Comment