Get mesh from two sets

Hoping someone knows how to accomplish what I am trying to accomplish:

I have a set, [item-a, item-b, item-c],

When I use setproduct([item-a, item-b, item-c], [item-a, item-b, item-c])

I get:

[
  [
    "item-1",
    "item-1",
  ],
  [
    "item-1",
    "item-2",
  ],
  [
    "item-1",
    "item-3",
  ],
  [
    "item-2",
    "item-1",
  ],
  [
    "item-2",
    "item-2",
  ],
  [
    "item-2",
    "item-3",
  ],
  [
    "item-3",
    "item-1",
  ],
  [
    "item-3",
    "item-2",
  ],
  [
    "item-3",
    "item-3",
  ],
]

Note the duplicates (i.e [“item-1”,“item-1”]) and the repeated sets (i.e both [“item-1”,“item-2”] and [“item-2”,“item-1”])

What I really want is:

[
  [
    "item-1",
    "item-2",
  ],
  [
    "item-1",
    "item-3",
  ],

  [
    "item-2",
    "item-3",
  ]
]

Hi @jay,

You can filter a collection by using a for expression with an if clause:

locals {
  example = toset([
    for pair in setproduct(var.a, var.a) : pair
    if pair[0] != pair[1]
  ])
}
1 Like