Skip to content

Instantly share code, notes, and snippets.

@chakrit
Created May 14, 2023 15:09
Show Gist options
  • Select an option

  • Save chakrit/a68bb5ea628839cfb61a19a6aae4f3c5 to your computer and use it in GitHub Desktop.

Select an option

Save chakrit/a68bb5ea628839cfb61a19a6aae4f3c5 to your computer and use it in GitHub Desktop.

Revisions

  1. chakrit created this gist May 14, 2023.
    80 changes: 80 additions & 0 deletions reconstruct.cue
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    input: {
    outer: {
    inner: [
    {item: "one", values: [3, 2, 1]},
    {item: "two", values: [1, 2, 3]},
    {item: "three", values: [7, 8, 9]},
    ]
    innernope: "innernope"
    }
    unrelated: "unrelated"
    }

    path: ["outer", "inner", 1, "values"]
    append: 4

    breakdown: [
    for idx, p in path {
    if idx == 0 {
    input[p]
    }
    if idx > 0 {
    breakdown[idx-1][p]
    }
    },
    ]

    reconstruct: [
    for idx, _ in path {
    if (int & path[len(path)-idx-1]) == _|_ {
    {
    if idx < len(path)-1 {
    for key, value in breakdown[len(path)-idx-2] if key != path[len(path)-idx-1] {
    "\(key)": value
    }
    }
    if idx == len(path)-1 {
    for key, value in input if "\(key)" != "\(path[len(path)-idx-1])" {
    "\(key)": value
    }
    }

    if idx == 0 {
    "\(path[len(path)-1])": breakdown[len(path)-1] + [append]
    }
    if idx > 0 {
    "\(path[len(path)-idx-1])": reconstruct[idx-1]
    }
    }
    }

    if (int & path[len(path)-idx-1]) != _|_ {
    [
    for key, value in breakdown[len(path)-idx-2] {
    if key != path[len(path)-idx-1] {
    value
    }
    if key == path[len(path)-idx-1] {
    if idx == 0 {
    breakdown[len(path)-1] + [append]
    }
    if idx > 0 {
    reconstruct[idx-1]
    }
    }
    },
    ]
    }

    },
    ]

    output: reconstruct[len(path)-1] & {
    outer: {
    inner: [
    {item: "one", values: [3, 2, 1]},
    {item: "two", values: [1, 2, 3, 4]},
    {item: "three", values: [7, 8, 9]},
    ]
    }
    }