Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting in Erlang (how do I increment a variable?)

Tags:

erlang

I've figured out the Erlang-style loops: tail-recursion with functions that take all the "variables that don't vary":

%% does something, 80 bytes at a time
loop(Line, File) -> loop(Line, File, 0).
loop(Line, File, Count) -> 
    do_something(Line, Count),
    case file:read(File, 80) of
        {ok, Line2} -> loop(Line2, File, Count + 1);
        eof -> file:close(File);
        {error, Reason} -> {error, Reason}
    end.

But, what is the best way to increment a counter in Erlang? In most programming languages, the way you count things is by incrementing a variable (ie. count += 1;). Erlang's variables don't vary, so we have to be creative. Fortunately, we have options...

We can pass a Counter variable with our functions, and increment it with each function call. We can use the process dictionary to store a count, and get and put to increment it. We can use ETS, the local data storage for processes. We can use a counter process (!!!):

loop(Count) ->                            
    receive                                   
        { incr } -> 
            loop(Count + 1);              
        { report, To } ->                     
            To ! { count, Count },            
            loop(Count)                           
    end.                                      

incr(Counter) ->
    Counter ! { incr }.

get_count(Counter) ->    
    Counter ! { report, self() },
    receive
        { count, Count } -> Count
    end.

I'm sure there are other ways too, depending on the scope. What's considered "best practice" for incrementing a variable in Erlang?

like image 913
Évariste Foucault Avatar asked Sep 29 '10 15:09

Évariste Foucault


People also ask

How do you increment a variable?

Using + and - Operators The most simple way to increment/decrement a variable is by using the + and - operators. This method allows you increment/decrement the variable by any value you want.


1 Answers

Don't use the process dictionary.

The 'normal' loop that you are expecting (ie a for loop or a do while) is usually implemented in a recursive function in Erlang so if you are incrementing a 'normal' counter do it in the function calls like you show up top.

Don't use the process dictionary.

In case you missed, can I just point out that you should not use the process dictionary.

like image 102
Gordon Guthrie Avatar answered Sep 20 '22 17:09

Gordon Guthrie