For the #twitcode contest (see: Twitter contest: what can you code in 130 characters?)
This Erlang code (98 characters) spawns N processes in a ring and sends a message through the entire ring. Try N=1000000 (1 million processes!!). In 135 characters, I could pass M messages through the ring, but there was no way to get that under 130. So this passes 1 message.
a(N)->X=b(self(),N),X!m. b(P,0)->P;b(P,N)->b(spawn(fun()->l(P)end),N-1). l(N)->receive T->N!T end.
Complete with module definition and export:
-module(r). -export([a/1]). a(N)->X=b(self(),N),X!m. b(P,0)->P;b(P,N)->b(spawn(fun()->l(P)end),N-1). l(N)->receive T->N!T end.
To run:
$ erl +P 2000000
Erlang R13B (erts-5.7.1) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]
Eshell V5.7.1 (abort with ^G)
1> c(r).
{ok,r}
2> P=spawn(fun()->r:a(1000000) end).
<0.39.0>
3>
It should run in under a minute (w/ 1 million processes) on most machines, maybe noticeably less. Obviously 1000 processes or so is instantaneous.
Technorati Tags: programming, twitter, twitcode, erlang



