title: What is the purpose of the state monad?
body: <p>I am a JavaScript developer on a journey to up my skills in functional programming. I recently ran into a wall when it comes to managing state. When searching for a solution I stumbeled over the state monad in various articles and videos but I have a really hard time understanding it. I am wondering if it is because I expect it to be something it is not.</p>
  
  <h3>The problem I am trying to solve</h3>
  
  <p>In a web client I am fetching resources from the back end. To avoid unnecessary traffic I am creating a simple cache on the client side which contains the already fetched data. The cache is my state. I want several of my modules to be able to hold a reference to the cache and query it for its current state, a state that may have been modified by another module.</p>
  
  <p>This is of course not a problem in javascript since it is possible to mutate state but I would like to learn more about functional programming and I was hoping that the state monad would help me.</p>
  
  <h3>What I would expect</h3>
  
  <p>I had assume that I could do something like this:</p>
  
  <pre><code>var state = State.of(1);
  map(add(1), state);
  state.evalState() // =&gt; 2 
  </code></pre>
  
  <p>This obviously doesn't work. The state is always 1. </p>
  
  <h3>My question</h3>
  
  <p>Are my assumptions about the state monad wrong, or am I simply using it incorrectly? </p>
  
  <p>I realize that I can do this:</p>
  
  <pre><code>var state = State.of(1);
  var newState = map(add(1), state);
  </code></pre>
  
  <p>... and <code>newState</code> will be a state of 2. But here I don't really see the use of the state monad since I will have to create a new instance in order for the value to change. This to me seems to be what is always done in functional programming where values are immutable.</p>