[erlang-questions] How to do counters?

Jarrod Roberson <>
Tue Jun 30 03:32:59 CEST 2009


2009/6/29 Witold Baryluk <>

> You can also use parametrized modules:
>
>
> -module(counter).
> -export([new/0]).
>
> new() ->
>        C = {counter,spawn(fun () -> loop(0) end)},
>        counter2:instance(Counter).
>
> loop(X) -> ...
>
>
> -module(counter, [Counter]). %% parametrized module
> -export([read/0, up/0, down/0, reset/0]).
>
> read() ->
>    ipc(Counter, read).
>
> ...
>
> Then just use:
>
> C1 = counter:new().
> C2 = counter:new().
> C1:up().
> C1:read().
> C2:read().


thanks for the responses guys I really appreciate the insight and help
getting my head around Erlang.

just for anyone else that has never heard of parametrized modules, I found
this interesting blog post:
http://userprimary.net/user/2009/03/04/two-erlang-finds-atomic-names-and-parameterized-modules/


More information about the erlang-questions mailing list