Skip to content

Commit 8e106bb

Browse files
committed
updates docs
1 parent 431a642 commit 8e106bb

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

arrayOperations.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ package go2
77
// func isEven(i int) bool {
88
// return i % 2 == 0
99
// }
10-
// el := FindOne(arr, isEven)
11-
// fmt.Println(el)
10+
// fmt.Println(FindOne[int](arr, isEven))
1211
// // output: [2]
1312
func FindOne[T any](arr []T, guard func(T) bool) (T, bool) {
1413
for idx := range arr {
@@ -33,9 +32,8 @@ func FindOne[T any](arr []T, guard func(T) bool) (T, bool) {
3332
// state[animal] = count + 1
3433
// return state
3534
// }
36-
// initialState := make([string]int)
37-
// finalState := Reduce(arr, countAnimals, initialState)
38-
// fmt.Println(finalState)
35+
// initialState := make(map[string]int)
36+
// fmt.Println(Reduce[string, map[string]int](arr, countAnimals, initialState))
3937
// // output: map["cat":2 "dog":1 "cow":1]
4038
func Reduce[T, A any](arr []T, fn func(A, T) A, init A) A {
4139
ret := init
@@ -53,8 +51,7 @@ func Reduce[T, A any](arr []T, fn func(A, T) A, init A) A {
5351
// func isEven(i int) bool {
5452
// return i % 2 == 0
5553
// }
56-
// newArr := Filter(arr, isEven)
57-
// fmt.Println(newArr)
54+
// fmt.Println(Filter[int](arr, isEven))
5855
// // output: [0,2,4]
5956
func Filter[T any](arr []T, guard func(T) bool) []T {
6057
var ret []T
@@ -74,8 +71,7 @@ func Filter[T any](arr []T, guard func(T) bool) []T {
7471
// func addTen(i int) int {
7572
// return i + 10
7673
// }
77-
// newArr := Map(arr, addTen)
78-
// fmt.Println(newArr)
74+
// fmt.Println(Map[int](arr, addTen))
7975
// // output: [11, 12, 13]
8076
func Map[T any](arr []T, transform func(T) T) []T {
8177
ret := make([]T, len(arr))
@@ -89,10 +85,8 @@ func Map[T any](arr []T, transform func(T) T) []T {
8985

9086
// Distinct returns the unique vals of a slice
9187
//
92-
// a := []int{1, 1, 2, 3}
93-
// b := Distinct[int](a)
94-
// fmt.Println(b)
95-
// output: [1, 2, 3]
88+
// fmt.Println(Distinct[int]([]int{1, 1, 2, 3}))
89+
// // output: [1, 2, 3]
9690
func Distinct[T comparable](arrs ...[]T) []T {
9791
// put the values of our slice into a map
9892
// the key's of the map will be the slice's unique values
@@ -116,9 +110,15 @@ func Distinct[T comparable](arrs ...[]T) []T {
116110

117111
// Intersect returns a slice of values that are present in all of the input slices
118112
//
119-
// [1, 1, 3, 4, 5, 6] & [2, 3, 6] >> [3, 6]
113+
// // example #1
114+
// a := []int{1, 1, 3, 4, 5, 6}
115+
// b := []int{2, 3, 6}
116+
// fmt.Println(Intersect[int](a, b))
117+
// // output: []int{3, 6}
120118
//
121-
// [1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
119+
// // example #2
120+
// fmt.Println(Intersect[int]([]int{1, 1, 3, 4, 5, 6}))
121+
// // output: []int{1, 3, 4, 5, 6}
122122
func Intersect[T comparable](arrs ...[]T) []T {
123123
m := make(map[T]int)
124124

@@ -155,9 +155,15 @@ func Intersect[T comparable](arrs ...[]T) []T {
155155

156156
// Union returns a slice that contains the unique values of all the input slices
157157
//
158-
// [1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 2, 4, 5, 6]
158+
// // example #1
159+
// a := []int{1, 2, 2, 4, 6}
160+
// b := []int{2, 4, 5}
161+
// fmt.Println(Union[int](a, b))
162+
// // output: []int{1, 2, 4, 5, 6}
159163
//
160-
// [1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
164+
// // example #2
165+
// fmt.Println(Union[int]([]int{1, 1, 3, 4, 5, 6}))
166+
// // output: []int{1, 3, 4, 5, 6}
161167
func Union[T comparable](arrs ...[]T) []T {
162168
m := make(map[T]struct{})
163169

@@ -182,9 +188,15 @@ func Union[T comparable](arrs ...[]T) []T {
182188

183189
// Difference returns a slice of values that are only present in one of the input slices
184190
//
185-
// [1, 2, 2, 4, 6] & [2, 4, 5] >> [1, 5, 6]
191+
// // example #1
192+
// a := []int{1, 2, 2, 4, 6}
193+
// b := []int{2, 4, 5}
194+
// fmt.Println(Difference[int](a, b))
195+
// // output: []int{1, 5, 6}
186196
//
187-
// [1, 1, 3, 4, 5, 6] >> [1, 3, 4, 5, 6]
197+
// // example #2
198+
// fmt.Println(Difference[int]([]int{1, 1, 3, 4, 5, 6}))
199+
// // output: []int{1, 3, 4, 5, 6}
188200
func Difference[T comparable](arrs ...[]T) []T {
189201
m := make(map[T]int)
190202

0 commit comments

Comments
 (0)