I am trying to solve a specific problem using functional programming. My guess is that a fold should do the job, but so far the solution has eluded me.
Starting from a dot-separated string like "a.b.c"
I want to build a Javascript object which in JS literal notation would look like:
obj = {a:{b:{c:"whatever"}}}
The algorithm should accept a seed object to start with. In the previous example the seed would be {}
.
If I provided {a:{f:"whatever else"}}
as seed, the result would be
{a:{f:"whatever else",b:{c:"whatever"}}}
I hope my description is clear enough. I am not talking about string manipulation. I want to create proper objects.
I am using Javascript because that's the language where this real-world problem has arisen and where I will implement the FP solution which I hope to find by asking here.
EDIT: the main issue I am trying to tackle is how to avoid mutable objects. JS is somehow too lenient about adding/removing attributes and in this case I want to be sure that there will be no side effects during the run of the FP routine.