Erlang

You are currently browsing the archive for the Erlang category.

The Erlyweb DJ app

I stayed up far too late last night playing with Erlang and friends – much longer than planned due to a few hitches. I did manage to complete more or less what I’d planned in the end though, so here is the documentation.

Read the rest of this entry »

Building on the steps in the previous parts of this series, I’ve added ErlyWeb into the equation. ErlyWeb is a web application framework built on Erlang/YAWS which I’ve been reading a bit about recently. As well as the main web site linked above, Yariv Sadan‘s blog is a good source of information.

Read the rest of this entry »

Part 4 of the series, documenting the setup of an Erlang test platform. In this episode, it’s YAWS (discussed previously here) that we’re adding.

Read the rest of this entry »

For part 3 of this ‘exciting’ series, I’m adding Jabberlang onto the existing setup to provide easy access to two-way XMPP messaging. If I was just sending, I would have restricted myself to tinkering with the code that Sam Ruby kindly piped up with the other day.

Read the rest of this entry »

Following on my previous post, the next stage is to add an XMPP server to the setup, in the form of ejabberd. As before, all the commands given are executing from our home directory, and in every case ubuntu01 should be substituted with the hostname (fully qualified) of the machine.

Read the rest of this entry »

For playing with distributed stuff, I’m using a bunch of Ubuntu ‘machines’ running under VMWare Server. There’s no particular reason for using Ubuntu other than that I happened to have a VM image (Ubuntu-6.06.1-desktop-i386) kicking around already, and it’s not really necessary to compile as packages are available, but I prefer it that way. Anyway, for reference, here is a quick guide to compiling Erlang from the starting point of a basic Ubuntu installation:

  1. From your home directory, get the Erlang source: wget http://www.erlang.org/download/otp_src_R11B-5.tar.gz
  2. Unpack it: tar -xzf otp_src_R11B-5.tar.gz
  3. Get a working gcc: sudo apt-get install build-essential
  4. Get the ncurses development libraries: sudo apt-get install libncurses5-dev
  5. Get m4: sudo apt-get install m4
  6. Optionally, get the ssl libraries: sudo apt-get install libssl-dev
  7. Go into the erlang source directory: cd otp_src_R11B-5
  8. Run the configuration script: ./configure
  9. Compile it all: make
  10. Install it: sudo make install

The above should all go smoothly, leaving you with a working Erlang setup. To confirm this, type erl to get an Erlang prompt.

1191028326_e69d7aa16e_o

Two quick snippets of unrelated randomness:

1. The things that were recently tasty eggs are now cute chicks, as of last Sunday, hence the picture. Only two though – one yellow, one black. The majority weren’t even fertile this time around, so perhaps Fred is losing his touch.

2. I was planning to go to bed two hours ago, then I started playing with Mnesia, which is Erlang’s distributed database. Very interesting indeed, but I need to stop doing it immediately and go to sleep instead.

YAW(N)S

Last post on this subject for now, hopefully, since even I’m getting bored of it. Just for the sake of completeness, I did the ISO date formatting ‘properly’, as follows:

w3cdtf(GregSecs) ->
	Date=calendar:gregorian_seconds_to_datetime(GregSecs),
	[UDate|_]=calendar:local_time_to_universal_time_dst(Date),
	USecs=calendar:datetime_to_gregorian_seconds(UDate),
	{{Y, M, D},{HH, MM, SS}} = Date,
	io_lib:format("~4..0b-~2..0b-~2..0bT~2..0b:~2..0b:~2..0b~s",
		[Y,M,D,HH,MM,SS,tzid(GregSecs-USecs)]).
 
tzid(Secs) when Secs==0 ->
	"Z";
tzid(Secs) ->
	{HH,MM,_}=calendar:seconds_to_time(abs(Secs)),
	io_lib:format("~c~2..0b:~2..0b",
		[case Secs >= 0 of false -> $-; true -> $+ end,HH,MM]).

As well as being shorter and clearer still, it doesn’t use the calendar:time_difference like the original did. That’s good because not only is that function very silly, it turns out it is marked as obsolete in the documentation. I suspect those two points are not unrelated.

Anyway, I’ll probably give the above some more rigourous testing and submit a patch. It may seem rather trivial, but actually it was a good exercise in getting to know Erlang a bit better. Half an hour of practical problem is usually worth ten of reading TFM.

YAWS continued

There was a suggestion in the comments that the code excerpt I showcased in my previous post was not shit at all, but rather just the way you do things in a functional programming language. By way of illustrating why it actually is shit, I present the following code that does exactly the same thing, using the same methodology:

less_shit(GregSecs) ->
	Date=calendar:gregorian_seconds_to_datetime(GregSecs),
	{{Y, Mo, D},{H, Mi, S}} = Date,
	[UDate|_] = calendar:local_time_to_universal_time_dst(Date),
	{DiffD,{DiffH,DiffMi,_}}=calendar:time_difference(UDate,Date),
	i2l(Y) ++ "-" ++ add_zero(Mo) ++ "-" ++ add_zero(D) ++ "T" ++
        add_zero(H) ++ ":" ++ add_zero(Mi) ++ ":"  ++
        add_zero(S) ++ tzid(DiffD,DiffH,DiffMi).
 
tzid(DiffD,DiffH,DiffMi) when DiffH < 12,  DiffH /= 0 ->
	"+" ++ add_zero(DiffH) ++ ":" ++ add_zero(DiffMi);
tzid(DiffD,DiffH,DiffMi) when DiffH > 12,  DiffD == 0 ->
	"+" ++ add_zero(DiffH) ++ ":" ++ add_zero(DiffMi);
tzid(DiffD,DiffH,DiffMi) when DiffH > 12,  DiffD /= 0, DiffMi /= 0 ->
	"-" ++ add_zero(23-DiffH) ++ ":" ++ add_zero(60-DiffMi);
tzid(DiffD,DiffH,DiffMi) when DiffH > 12,  DiffD /= 0, DiffMi == 0 ->
	"-" ++ add_zero(24-DiffH) ++ ":" ++ add_zero(60-DiffMi);
tzid(DiffD,DiffH,DiffMi) when DiffH == 0 -> "Z".

The point to note is that no complex expression is repeated multiple times, and nor is the actual logic of generating the timezone identifier buried within one. It’s much clearer, and much shorter.

Another point to note is that it is still shit, and I make no apologies for that, as firstly I wanted to keep the methodology the same, and secondly I am still getting to grips with Erlang.

YAWS

I said I would have another look at YAWS, given the apparently amazing performance. I decided to dive straight into the source, since I had more than a hunch that anything substantial written in Erlang would be an unmaintainable mass of crap. I certainly wasn’t disappointed in this case. The following excerpt is one of several chunks of code I came across that do exactly* the same thing:

Read the rest of this entry »

« Older entries § Newer entries »