Modifying a nested data structure?
See original GitHub issueThis isn’t an issue but really a question about if/how partial.lenses could be used to solve a data transformation that I am working on.
I have a nested data structure that I am working on that should be 3 levels deep; let’s call the levels “rows”, “columns”, and “cells” like a table. So I might have a structure like:
[
{
value: "a",
nodes: [
{
value: "b",
nodes: [
{
value: "c",
nodes: []
},
// ...etc
]
},
// ...etc
]
},
// ...etc
]
Sometimes however, the rows or columns levels might be missing. So I may just get back a single level or two levels. In those scenarios, I want to fill the gaps in tree with a default value. So lets say that I received the following data structure:
[
{
value: "c",
nodes: []
},
// ...etc
]
I want to turn that into something like:
[
{
value: "DEFAULT_ROWS_VALUE",
nodes: [
{
value: "DEFAULT_COLUMNS_VALUE",
nodes: [
{
value: "c",
nodes: []
},
// ...etc
]
}
]
}
]
Is this kind of transformation possible with this lib? I’ve struggled to figure out how I could dynamically accomplish this. I do have flags that let me know if columns and rows are present in the returned data or not. Thanks for any advice
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (6 by maintainers)
Thanks for taking the time to put these together, they are really helping me learn.
I see what you are saying about not leveraging optics if just doing a transformation in one direction, which is the case here for sure. I do leverage optics for bidirectional things in several places in my application, but I think I then got used to using
L
and started reaching for it in places where it wasn’t necessary. Probably a good opportunity for me to clean up some code. Thanks again for the feedbackSomething like this.
Here is a similarly structured solution using only ordinary functions.
It sounds like in this case you need to perform a whole data structure transformation only in one direction. Without knowing more, I probably wouldn’t recommend using optics in this particular case.