Skip to content

Commit cc4a462

Browse files
committed
add a go example
1 parent ed8972f commit cc4a462

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

coderd/util/slice/example_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package slice_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/coder/coder/v2/coderd/util/slice"
7+
)
8+
9+
func ExampleSymmetricDifference() {
10+
// The goal of this function is to find the elements to add & remove from
11+
// set 'a' to make it equal to set 'b'.
12+
a := []int{1, 2, 5, 6}
13+
b := []int{2, 3, 4, 5}
14+
add, remove := slice.SymmetricDifference(a, b)
15+
fmt.Println("Elements to add:", add)
16+
fmt.Println("Elements to remove:", remove)
17+
// Output:
18+
// Elements to add: [3 4]
19+
// Elements to remove: [1 6]
20+
}

coderd/util/slice/slice.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func Descending[T constraints.Ordered](a, b T) int {
113113
// In classical set theory notation, SymmetricDifference returns
114114
// all elements of {add} and {remove} together. It is more useful to
115115
// return them as their own slices.
116+
// Notation: A Δ B = (A\B) ∪ (B\A)
116117
// Example:
117118
//
118119
// a := []int{1, 3, 4}

0 commit comments

Comments
 (0)