Travis

Travis

Implementing a counter, aka: how to keep state in a closure

Completely new to Elixir, but have an issue I am trying to figure out:

When interviewing candidates for Javascript roles I usually have them do a simple technical question:
counter1 = getCounter(1);
counter2 = getCounter(17);
counter1(); //gives 1
counter1(); //gives 2
counter2(); //gives17
counter1(); //gives 3

And the challenge is “Implement the function getCounter”. For candidates who understand functions and closures this takes 4-6 lines and less than a minute, and is generally impossible for everyone else, so is a very efficient sort. For new languages, I try building a version of this to get a sense of how anonymous functions and closures work (or don’t) in the language. I have not been able to solve this for Elixr. It is functional, it has closures, you CAN return functions, so it seems this should not only work, but be trivial…

Javascript:
getCounter = function(initVal) {
i = initVal - 1 //Fix off by 1
return function() {
i = i +1;
return i;
}
}

Go:
func getCounter(initVal int) func() int {
i := initVal - 1
return func() int {
i += 1
return i
}
}

Note that you do NOT know beforehand how many times your anonymous counter might be called (or when), nor what start value you might get. I have seen recursion mentioned quite a bit, but that would require that you know how many times you want to call your counter when you start. What is the Elixir way to do this properly?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The problem definition seems to assume mutable state so there’s a bit of a mismatch here. It’d be a bit like saying “In every language I want to learn I look at how to set register X0 to 1 and then increment it, how do I do that with JS?”. Javascript doesn’t manipulate registers, and Elixir doesn’t mutate values in closures.

Nonetheless, You can simulate mutable state with a process, and the Agent module provides a handy API in this case.

iex(6)> {:ok, pid} = Agent.start_link(fn -> 0  end)                     
{:ok, #PID<0.113.0>}
iex(7)> fun = fn -> Agent.get_and_update(pid, fn i -> {i, i + 1} end) end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(8)> fun.()                                                           
0
iex(9)> fun.()
1
iex(10)> fun.()
2
iex(11)> fun.()
3
iex(12)> fun.()
4

I’ll leave the code to make two counters as an exercise for the reader

NobbZ

NobbZ

Due to the way elixir works, you have to return an updated closure as well on each call (untested):

def get_counter(i \\1) do
  fn -> {i, get_counter(i + 1)} end
end

Usage is roughly like this:

c = get_counter(17)
{v, c} = c.()
#=> {17, ...}
{v, c} = c.()
#=> {18, ...}
Nicd

Nicd

To expand a bit on the reason why you need to do it in a way that may not be intuitive for a first time functional programmer: Data in Elixir is immutable. Immutabililty means that once you have created a piece of data, it can’t be changed.

Trying to simply create an anonymous function that adds to a counter like this won’t work:

c = 0
f = fn -> c = c + 1; c end
IO.inspect(f.()) # 1
IO.inspect(f.()) # Still 1
IO.inspect(f.()) # Even still 1

That’s because the data in variable c is immutable. Inside the function the code can rebind the variable c to point to another piece of data, but the original c outside the function does not change. Even if you set c to point to a new value outside the function, it still would not affect any earlier usage of the variable. So the c that is bound to the context of the closure f is always 1 and will be.

So that is why you mainly have two avenues to solve this problem. Either you can create a new process (like the Agent suggested by benwilson512), or you can create a new function with the updated value on each call and use the updated function on the next call, like NobbZ suggested.

You might wonder how a process can hold state and update it if data is immutable. That’s because a process basically runs an infinitely recursing function that it gives the updated state to for the next iteration. So (very much simplified) a process looks like this:

def run_process(state) do
  receive do
    msg ->
      new_state = update_state(msg) # Do something based on the state and return the new state
      run_process(new_state) # Recurse with the new state, effectively updating the state of the process
  end
end

You can see there that no immutability guarantees are broken and still the state is updated.

Hope this helps and doesn’t confuse further. :sweat_smile:

amarraja

amarraja

The attached article is a great read about state in Elixir. Interestingly, the examples used are pretty much the same as yours.

http://dantswain.herokuapp.com/blog/2015/01/06/storing-state-in-elixir-with-processes/

Where Next?

Popular in Questions Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement