Azure / bicep

Bicep is a declarative language for describing and deploying Azure resources
MIT License
3.21k stars 742 forks source link

Addition of some missing "set theory" array functions. #9953

Open lzandman opened 1 year ago

lzandman commented 1 year ago

Today I had to do some array juggling and was looking for Bicep functions to assist me. I found the concat, intersection and union functions particularly helpful. However, some "set theory" functions seemed notably absent.

In particular I was looking for a difference/complement/except function except(A, B), that would return all elements present in array A, but not in array B. Like this:

param firstArray array = [
  'one'
  'two'
  'three'
  'four'
]

param secondArray array = [
  'three'
  'four'
  'five'
]

output arrayOutput array = except(firstArray, secondArray)
The output would be: Name Type Value
arrayOutput Array ["one", "two"]

Another would be the symmetric difference/disjunctive union of arrays, symmetricExcept(A, B), that would return all elements present in array A and array B, but not in both. Like this:

param firstArray array = [
  'one'
  'two'
  'three'
  'four'
]

param secondArray array = [
  'three'
  'four'
  'five'
]

output arrayOutput array = symmetricExcept(firstArray, secondArray)
The output would be: Name Type Value
arrayOutput Array ["one", "two", "five"]

Of course there are more "set theory" functions (carthesian product comes to mind) missing. And maybe these proposed functions can be realized using some alternative Bicep code/lambda expressions. It sure would've helped me today if these functions were already available in Bicep, so I thought I'd add this feature suggestion. My sample is based on arrays, but just like some of the already existing functions, I imagine they should also be available for objects.

anthony-c-martin commented 1 year ago

It would also be worth covering distinct(<array>) with this task. Currently I believe a workaround is achievable with unique(<array>, <array>), but this isn't very discoverable.