79111654

Date: 2024-10-21 20:29:47
Score: 0.5
Natty:
Report link

Developing @lucas Ross answer:

You make it on a one line command :

 distinct_tuples = [for k, v in { for t in <your_tuples> : t.a => t... } : v[0]]

Explanation: The first loop will group by each tuple that have the same "a" attribute. This is done using ... symbol. You will have the following Map of objects:

  map     = {
    "1" = [
        {
          a = 1
          b = 2
        },
        {
          a = 1
          b = 3
        },
    ]
    "5" = [
        {
            a = 5
            b = 4
        },
    ]
}

for the second loop, it will flatten the map created previously and selects only the first value. you will get the following object:

[
    {
        a = 1
        b = 2
    },
    {
        a = 5
        b = 4
    },
]

references:

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @lucas
  • Low reputation (0.5):
Posted by: ilyesAj