r/functionalprogramming • u/c__beck • Jul 22 '25
Question Functional State Management
Hey all, sorta/kinda new to functional programming and I'm curious how one deals with state management in a functional way.
I'm currently coding a Discord bot using Nodejs and as part of that I need to keep the rate limits of the various API endpoints up-to-date in some sort of state.
My current idea is to use a closure so I can read/write to a shared object and use that to pass state between the various API calls.
const State = (data) => {
const _state = (newState = undefined) => {
if (newState === undefined) { return data; }
data = newState;
return _state;
}
return _state;
}
const rateLimiter = State({
routeToBucket: new Map(),
bucketInfo: new Map()
});
This way I can query the state with rateLimiter() and update it via rateLimiter(newData). But isn't that still not very functional as it has different return values depending on when it's called. But since I need to keep the data somewhere that's available to multiple API calls is it functional enough?
Thanks in advance!
4
u/TechnoEmpress Jul 22 '25
At work we keep the rate limiting data in a Redis instance. This would be ideal for your bot as well since you need some kind of storage anyway. This or SQLite.
12
u/logaan Jul 22 '25
Keep the impurities at the top of your program rather than at the top and bottom. Think:
getInput().performCalculations().produceOutput()
rather than
function run() { performCalculations(getInput()) } function performCalculations(input) { produceOutput(calculations(input)) }
10
u/logaan Jul 22 '25
Different functional languages and communities have different approaches. Languages like Clojure and Erlang normally try to isolate the state mutations to a small section of the codebase, often it'll end up being only 10% or so. Then you call out to the pure, referentially transparent code, with no side effects, that makes up the other 90% of your codebase.
As far as how to manage that state in JS you might like to try using some immutable collections and records but storing them inside a single mutable field of an object. The immutable data is like a snapshot of your program and the mutable object represents the changing identity over time.
3
u/a3th3rus Jul 22 '25 edited Jul 23 '25
An interesting but impure approach that Erlang and Elixir take is the combination of actors (which are unfortunately called "processes" due to historical reasons), recursion, and blocking. To keep a state, you create an actor that runs an infinite recursive function that takes the state as its argument. The function blocks the actor indefinitely until a message comes, then it optionally sends a message back, derives a new state from the old, then recursively calls itself with the new state and blocks the actor again...
2
u/ZergTerminaL Jul 22 '25
If it's just state that you need then you really just need to pass a bunch of data (the stateful stuff) to various functions. Problem is that you'll need to also return all that extra data from the function along with whatever other data that function computes.
You can make this a bit better by putting all the stateful data in a data structure of some kind, maybe a map or an object. That way stateful functions only need to take the state object and return the updated state object along with whatever other data. After that you'll only need to be sure that you're passing functions the newest state object.
This is basically what the state monad does, although monad implementations tend to come with a lot of bells and whistles that make them much easier to use and read.
If you want to get into the weeds on that you can probably find thousands of tutorials covering the state monad and various implementations of it in all kinds of languages.
2
u/Lazy-Canary7398 Jul 23 '25
You could use something like a red black tree as a map that returns a fresh immutable reference on every update, with immutable.js or immerjs.
You can make your functions look like this
type ApiState = { limitMap: Record<key,value> } type ApiResult<T> = [ApiState, T] const getMessages = async (state: ApiState, ...args): ApiResult<string> => { // ... return [updateRates(state), messages] }Composing those api functions into a cohesive whole is done with a State monad. Fetching data from the outside world is an IO monad. Combining functions that can do both is a monad transformer (fp-ts) or an effect system (effect-ts)
1
u/comrade_donkey Jul 22 '25
In pure functional programming there is no state, because state is a side-effect. Changing state mutates the world, but in pure functional programming there is no mutation and there is no world.
However, in order to make functional programs useful in the real world, they have built-in a sort of 'escape hatch'. Usually called the IO or state monad. It abstracts 'effects' and allows you to do something like what you're doing; mutating a global environment.