This is more of a theoretical question but I feel like there must be a way to do this.
I have JS components for which, when they are created, they need to assign a unique id to an html element, one that hasn't been used in any other component. This is pretty trivial normally:
let currentId = 0;
function getNextId() {
currentId += 1;
return currentId;
}
function MyComponent() {
this.id = getNextId();
// Code which uses id
}
let c1 = new MyComponent();
// c1.id === 1
let c2 = new MyComponent();
// c2.id === 2
I'm wondering if there's any way to do this kind of thing using just pure functions, as I'm trying to wrap my head around some more advanced pure functional ideas. As far as I can tell it would require Monads or something of that variety to accomplish, but I don't know how to do so.
Thank you!