Frankly speaking, I always use apt-get to install Erlang rather than building from source… lazy huh? Unfortunately, when I apt-get in Ubuntu 9.10 Karmic, only Erlang/OTP R13B01 (5.7.2) is available whereas the latest is R13B04 (5.7.5) which is required to build Riak (actually rebar requires 5.7.4 and above)…

It takes around 10-15 min to build, and these are the required libraries and dependencies

It’s been a long time I haven’t blogged anything about erlang … damn… too many cool stuff but too little time. Actually my works are getting heavily busy these days + 4-month-old kid … lots of excuses huh?

OneTeam Media Server, or OMS for short, is a new Flash server implementation, written in erlang, by ProcessOne. This will enable your users to broadcast voice and/or video streams to multiple subscribers.

Wow, this is a great news … it’s greater to see more and more heavy task back-end services written in Erlang. I’ve registered an account with ProcessOne Git Repository just to fork the project. Lots of thing can learn from OMS. Many thanks to ProcessOne for open source this project.

For AMF implementation, I have to define the mapping between Actionscript classes with something equivalent in Erlang. First thing came into my mind is record. However, I immediately encountered some of the difficulties when using records in Erlang

Read: can’t use reflection easily after parsing AMF, I received the object with properties/values and I need to map to the corresponding record

Write: Need to introspect the record and write its properties into binaries

Normally records are defined in a header file “.hrl”. And the idea is to generate utility codes (getter, setter, record meta data ….) at compile time. The heart of it is epp:parse_file/3 function. The generator (or the helper) reads header files, parses all records and create a compilable .erl file which contains all functions you need to do all magic with records:

set(Obj, PropertyName, Value) -> {ok, NewObj, {PropertyName, Value}}

get(Obj, PropertyName) -> {ok, Value}

fields(record_name) -> Fields = [string()]

fields_atom(record_name) -> Fields = [term()]

type(Obj) -> record_name

messages.hrl – the source

record_helper.erl – the generator

record_utils.erl – the generated source

Below is the demonstration of how to generate and use the record utils

I just restarted a long sleep idea to implement AMF using Erlang, the project, codenamed Erling, is hosted in github. There’s nothing much yet except amf0 and amf3 implementation, i’m working on the unit test for whatever I’ve written so far to make sure it won’t go messy as the code evolved.
Everyday I’m learning new thing along with Erling and therefore more Erlang examples to come …

Erlang offers calendar module which provides computation of date, time and number of date time conversion functions. For more details, please refer to documentation for this module
Here are some examples:

Quick Sort

I took this example from Joe Armstrong’s book: Programming Erlang to show the beauty Erlang in recursion. Classic example of quick sort implementation

Can’t believe it, 6 simple and very elegant!!! I remembered roughly 10 years ago, how hard to write quick sort implementation in Pascal.

Tail Recursion

The quick sort implementation above is not tail recursive way. As the function call doesn’t return the whole results at once (look at the list concatenation). Here are few examples of tail recursion in Erlang

As I remember the first time I was tought about Object-Oriented paradigm, my teacher told me “everything around you is an object” … Damn, since then I couldn’t get rid of that even when programming with Erlang.

People say Records concept in Erlang is very similar to struct in C. That’s true, but for me, it looks rather like an Object…

Normally, records are defined in a separate file with extension .hrl and included in the main module file.

person.hrl

%% person.hrl
-record(person, {name, age}).

Here I define a record, named person, which has 2 attributes name and age. I’ll use erl console to do some manipulation with record person

1> I read record person defined in person.hrl
2> I created a new “object” of type person and assigned to variable X
3> I printed value of the attribute name from “object” X
4> I printed value of the attribute age from “object” X
5> I used pattern matching to extract value of the attribute name out of “object” X and assigned to variable Name
6> I printed value of variable Name