r/functionalprogramming • • 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

22 comments sorted by

View all comments

Show parent comment

3

u/Historical-Essay8897 Apr 22 '25 edited Apr 22 '25

if you have an object with mutable state, then when you use or modify it you need to keep track of all other parts of the program that can change it to avoid bugs. If however you only 'mutate' by assigning/updating to another variable then you don't need to worry about remote changes to the object you are using. That is why FP happily passes state as arguments and returns as values but avoids mutation in-place where possible. In most FP languages assigning to the same variable again creates a new (shadowed) variable rather than overwriting, and does not affect the original value.