Erlang is pretty damn awesome, and I’ve been self-teaching it over the last month. The language is functional, fault-tolerant, super-concurrent, and easily run distributed over multiple computers. That’s a whole lot of awesome.
So, I needed a project to cut my teeth in, something simple, with nice, clear end goals. So I ported my Siege Unit Converter, a program I wrote ages ago to convert obscure MUT files into simple BMP files. Now, this wasn’t the ideal use-case for Erlang (converting images from one format to another is something that’s quite suited to imperative step-by-step code), but it’s a small project that doesn’t need anything especially complex from a language, so it was perfect for testing my Erlang skills.
Wow. I went down from 566 lines of code to 135 (inclusive of comments, according to the Unix wc), and the majority of them are for producing error messages (the average Erlang program wouldn’t need to, as it’d allow the process to crash and get another process to handle the error, for nice, clean code).
Look at this from the C++ version. I admit, I wrote it a while ago, and probably isn’t the best. It’s for converting the Siege palette file into something the BMP can read.
for(int i = 0; i < 256; ++i)
{
bmpFile.Write(palette[(i*3)+2]); //Red //Colours are stored as BGR in Siege's palette - backwards again
bmpFile.Write(palette[(i*3)+1]); //Green
bmpFile.Write(palette[(i*3)]); //Blue
bmpFile.Write(0); //Unused - bitmaps pad the alpha channel to keep things in a nice DWORD size
}
Now look at the Erlang for the same function.
parse_palette_group(Palette_out, <<>>) ->
reverse(Palette_out);
parse_palette_group(Palette_out, <>) ->
parse_palette_group(<<0, B, G, R, Palette_out/binary>>, Rest).
Erlang's code cuts out all of the horrid trying-to-work-out-the-position code, and just happily builds the data structure. Amazing.
Now, the one issue I've had is due to the sequential nature of the source program - Erlang doesn't support this well, as each IF-stye block (Case, in Erlang's case) had to be a nested level - for neatness, I had to split the functions into multiple steps. However, I managed to add a function to take a list of files that would happily convert them all - and it only took two lines.
Once I've cut my teeth on some of the networking stuff, I'll be putting together something big, concurrent and awesome with the language. Something with spaceships and shooting, and lots of players.
You can download the code HERE