AmanKing

ErlangControl PanelChange LogBrowse PagesSearch?

Erlang

My first tryst with Erlang in an Object Bootcamp conducted at ThoughtWorks was pretty pleasant. I'm quite impressed actually. I look forward to exploring it a bit more when I can. Will blog about it then.

Right now, just check out this code (it's probably not idiomatic but hey, I think it's decent):

-module (coffeemaker).
 
-compile (export_all).
 
coffee_maker() ->
	receive
		{Pid, {make, Coffee}} -> 
			io:format("making ~p for ~w...~n", [Coffee, Pid]),
			Pid ! {self(), {completed, Coffee}},
			coffee_maker();
		{Pid, Any} ->
			io:format("Unrecognized message ~p ~n", [Any]),
			Pid ! {self(), {error_dont_understand, Any}},
			coffee_maker();
		Any ->
			io:format("Unrecognized message ~p ~n", [Any]),
			coffee_maker()
	end.
 
 
customer(Pid, Coffee) ->
	Pid ! {self(), {make, Coffee}},
	receive
		{Pid, {completed, Coffee}} ->
			io:format("yeeepieee! got my ~p from ~w~n", [Coffee, Pid]);
		{Pid, Any} ->
			io:format("Unrecognized message ~p ~n", [Any]),
			Pid ! {self(), {error_dont_understand, Any}};
		Any ->
			io:format("Unrecognized message ~p ~n", [Any])
	end.


To run the above, fire up the Erlang console:

Erlang (BEAM) emulator version 5.5.5 [async-threads:0]

Eshell V5.5.5  (abort with ^G)
1> c(coffeemaker).
{ok,coffeemaker}
2> Coffee_maker=spawn(fun()->coffeemaker:coffee_maker() end).
<0.37.0>
3> coffeemaker:customer(Coffee_maker, "Mocha").
making "Mocha" for <0.30.0>...
yeeepieee! got my "Mocha" from <0.37.0>
ok
4> 

There's a lot of other nifty stuff we can do with Erlang (think "nodes"). I'll probably blog about them some other time.

Related Links


Tags: technology:erlang, technology:emerging Last modified 11:51 Fri, 18 Apr 2008 by AmanKing. Accessed 197 times Children What Links Here share Share Except where expressly noted, this work is licensed under a Creative Commons License.