r/functionalprogramming • u/Level_Fennel8071 • Apr 21 '25
Question mutable concept confusion
hello,FP newcomer here, is this considered to be functional? if not how can we implement global state in any application.a good resources that explain this part is appreciated
// Pure function to "increment" the counter
const increment = (count) => count + 1;
// Pure function to "decrement" the counter
const decrement = (count) => count - 1;
// Pure function to "reset" the counter
const reset = () => 0;
// Example usage:
let count = 0;
count = increment(count); // count is now 1
count = increment(count); // count is now 2
count = decrement(count); // count is now 1
count = reset(); // count is now 0
10
Upvotes
View all comments
Show parent comment
2
u/OddInstitute Apr 22 '25
To be clear, the approach you are taking here with taking the counter value as an argument and returning a new counter value from any function that would otherwise mutate it is all the state monad does. It’s not particularly magical.
As you could say imagine, this sort of approach gets pretty unwieldy as you increase the amount of possible state or increase the complexity of your logic. Phasing these sorts of operations in terms of the monad interface (along with some state-specific operations built with that interface) really simplifies this approach to coding. For example, it gives tools for turning functions that don’t interact with the state in to functions that take the state as an extra argument and return it unchanged. The basic monad abstraction also provides functions that handle all of the unwrapping and wrapping involved in actually operating on these state values as though they were global variables.
Coding to this interface also allows for things like the ST type which behaves the same as the State type, but actually mutates the state variables in order to improve performance. Since all of your code is still factored through the monad interface (and more importantly still uses a very powerful static type system), you can’t tell the difference at run time between the code that mutates and the code that doesn’t.
All that said, using a pure functional language as a mutable imperative language has many of the same problems as just using an imperative language to begin with. The state values are more constrained and easier to inspect, but the order your functions get called in and how many times they get called still matters. You still have to track state mutation if you want to debug your code.
There are some upsides though. Any code that doesn’t take the state as an input variable definitely doesn’t interact with it. If you phrase things in terms on the monad interface, you can also pretty easily get access to all possible state mutations or mock out interactions for testing in ways that would be super annoying and invasive in most imperative languages.
Finally, a lot of the interesting and useful bits of functional programming involve ways of thinking about computation that are completely different from state mutation and you will learn a lot if you take the time to learn a language that is designed to support those techniques. There are problems for which state mutation is the best known approach though, so techniques like you explore here remain useful to have in the tool bag.