Skip to content

Commit 25bfe73

Browse files
lang: add setsubtract function (hashicorp#23424)
* add setdifference and setsubtract functions and docs * remove setdifference as it is not implemented correct in underlying lib * Update setintersection.html.md * Update setproduct.html.md * Update setunion.html.md
1 parent b4f21b6 commit 25bfe73

7 files changed

Lines changed: 66 additions & 0 deletions

File tree

lang/functions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func (s *Scope) Functions() map[string]function.Function {
100100
"rsadecrypt": funcs.RsaDecryptFunc,
101101
"setintersection": stdlib.SetIntersectionFunc,
102102
"setproduct": funcs.SetProductFunc,
103+
"setsubtract": stdlib.SetSubtractFunc,
103104
"setunion": stdlib.SetUnionFunc,
104105
"sha1": funcs.Sha1Func,
105106
"sha256": funcs.Sha256Func,

lang/functions_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,15 @@ func TestFunctions(t *testing.T) {
687687
},
688688
},
689689

690+
"setsubtract": {
691+
{
692+
`setsubtract(["a", "b", "c"], ["a", "c"])`,
693+
cty.SetVal([]cty.Value{
694+
cty.StringVal("b"),
695+
}),
696+
},
697+
},
698+
690699
"setunion": {
691700
{
692701
`setunion(["a", "b"], ["b", "c"], ["d"])`,

website/docs/configuration/functions/setintersection.html.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ the ordering of the given elements is not preserved.
4040
a given element value.
4141
* [`setproduct`](./setproduct.html) computes the _Cartesian product_ of multiple
4242
sets.
43+
* [`setsubtract`](./setsubtract.html) computes the _relative complement_ of two sets
4344
* [`setunion`](./setunion.html) computes the _union_ of
4445
multiple sets.

website/docs/configuration/functions/setproduct.html.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,6 @@ elements in the input variables.
224224
object types are defined explicitly.
225225
* [`setintersection`](./setintersection.html) computes the _intersection_ of
226226
multiple sets.
227+
* [`setsubtract`](./setsubtract.html) computes the _relative complement_ of two sets
227228
* [`setunion`](./setunion.html) computes the _union_ of multiple
228229
sets.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
layout: "functions"
3+
page_title: "setsubtract - Functions - Configuration Language"
4+
sidebar_current: "docs-funcs-collection-setsubtract"
5+
description: |-
6+
The setsubtract function returns a new set containing the elements
7+
from the first set that are not present in the second set
8+
---
9+
10+
# `setsubtract` Function
11+
12+
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
13+
earlier, see
14+
[0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html).
15+
16+
The `setsubtract` function returns a new set containing the elements from the first set that are not present in the second set. In other words, it computes the
17+
[relative complement](https://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement) of the first set in the second set.
18+
19+
```hcl
20+
setsubtract(a, b)
21+
```
22+
23+
## Examples
24+
25+
```
26+
> setsubtract(["a", "b", "c"], ["a", "c"])
27+
[
28+
"b",
29+
]
30+
```
31+
32+
### Set Difference (Symmetric Difference)
33+
34+
```
35+
> setunion(setsubtract(["a", "b", "c"], ["a", "c", "d"]), setsubtract(["a", "c", "d"], ["a", "b", "c"]))
36+
[
37+
"b",
38+
"d",
39+
]
40+
```
41+
42+
43+
## Related Functions
44+
45+
* [`setintersection`](./setintersection.html) computes the _intersection_ of multiple sets
46+
* [`setproduct`](./setproduct.html) computes the _Cartesian product_ of multiple
47+
sets.
48+
* [`setunion`](./setunion.html) computes the _union_ of
49+
multiple sets.

website/docs/configuration/functions/setunion.html.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ the ordering of the given elements is not preserved.
4545
multiple sets.
4646
* [`setproduct`](./setproduct.html) computes the _Cartesian product_ of multiple
4747
sets.
48+
* [`setsubtract`](./setsubtract.html) computes the _relative complement_ of two sets

website/layouts/functions.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@
222222
<a href="/docs/configuration/functions/setproduct.html">setproduct</a>
223223
</li>
224224

225+
<li>
226+
<a href="/docs/configuration/functions/setsubtract.html">setsubtract</a>
227+
</li>
228+
225229
<li>
226230
<a href="/docs/configuration/functions/setunion.html">setunion</a>
227231
</li>

0 commit comments

Comments
 (0)