Ayende @ Rahien

It's a girl

Challenge: What does this code do?

What language is it? What is it doing? Why?

image

Comments

Iain
03/19/2010 10:18 AM by
Iain

It looks like a functional language, maybe f# or Clojure returning a list messages

Ben in Japan
03/19/2010 10:19 AM by
Ben in Japan

Google tells me that it's Erlang.I can't understand it for the life of me, but It looks like a small server process, maybe doing unnatural acts on processes.

Andreas Schlapsi
03/19/2010 10:29 AM by
Andreas Schlapsi

This is Erlang code. I'm not fluent in Erlang, but I think it starts some Erlang processes and delegates handling of messages to one of those services. Every time this process receives a message it delegates the message to the next "child process".

Oliver
03/19/2010 11:09 AM by
Oliver

Don't know Erlang either, but I think I spot some syntax errors/typos:

{stop, normal, State ); --> {stop, normal, State }.

an in handle_call() I guess there's a } missing at the end before the dot, as well as a (maybe optional?) comma in terminate() after ... = State.

I might be wrong though.

Damien
03/19/2010 12:09 PM by
Damien

its lambda expressions in F# I think but am not sure

Ivica Munitic
03/19/2010 12:15 PM by
Ivica Munitic

It is a map/reduce algorithm written using the gen_server behavior from the OTP erlang library.

Rafal
03/19/2010 12:22 PM by
Rafal

Wow, so that's THE map/reduce... I'm totally disappointed, after having heard so much about it i expected it to have at least 200 lines of code.

cadet354
03/19/2010 01:35 PM by
cadet354

load balancer in erlang code.

Ayende Rahien
03/19/2010 01:52 PM by
Ayende Rahien

Cadet354,

Great! You got it!

Now, how does it work? :-)

cadet354
03/19/2010 02:20 PM by
cadet354

init runs Num threads.

handlecall (Msg, From, State) handled asynchronously (genserver: cast) request.

(Processes, (CurrentIndex +1) rem Num, Num) selects the next thread that will work.

P.S. I apologize for my bad English

Ayende Rahien
03/19/2010 02:23 PM by
Ayende Rahien

Cadet354,

I'll quibble with threads vs. processes, but you are correct.

cadet354
03/19/2010 02:25 PM by
cadet354

Yes, lightweight erlang threads, not OS threads.

Robert M.
03/19/2010 03:59 PM by
Robert M.

@Ayende: Is it something that you've built or did you find the code in some other app?

If you built it, for what project do you use it? I am thinking about Erlang course that we've participated one week ago :-)

Ayende Rahien
03/19/2010 06:21 PM by
Ayende Rahien

It was a sample code that I had written in that course.

Alexey Romanov
03/20/2010 06:03 AM by
Alexey Romanov

Using lists:nth in this way is a bad idea. Quadratic behaviour ahoy!

Possible fixes:

1) switch to using arrays or one of the dictionary libraries

2) change your state to keep two lists: the processes you haven't used this round yet, and the ones you have. When the first list runs out, reverse the second and use it again.

Additionally, it's now trivial to add more processes, if desired.

masklinn
03/20/2010 04:02 PM by
masklinn

@Oliver

Your first correction is wrong, the other two are right

Comments have been closed on this topic.