question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modifying a nested data structure?

See original GitHub issue

This 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:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
skokenescommented, May 31, 2019

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 feedback

0reactions
polytypiccommented, May 31, 2019

Something 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modifying Deeply-Nested Structures - nvie.com
First and foremost, we need a way of traversing arbitrarily nested structures. Given that this is only JSON data (so limited to strings, ......
Read more >
5.5. Working with Nested Data Structures
Storing information in a nested data structure has become a common practice. This allows information to be collected in one structure while still...
Read more >
Modify an Object Nested Within an Object - Free Code Camp
In this basic data structures tutorial we modify an object nested within an object. This makes up one part of many to conclude...
Read more >
Nested Data Structures
A function built into Python that is always available for use. This creates a new list every time, so when the list gets...
Read more >
Nested Data Structures and Nested Loops | by Diane Tunnicliffe
So with that in mind, here is some work with nested data in Python using nested loops. I will start with some data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found