1
1
package arrays
2
2
3
- //The efficiency of this algorithm is O(N) but it reverses the list. Use FoldLeft instead if you don't want this.
3
+ // The efficiency of this algorithm is O(N) but it reverses the list. Use FoldLeft instead if you don't want this.
4
4
func FoldRight [T1 , T2 any ](as []T1 , z T2 , f func (T1 , T2 ) T2 ) T2 {
5
5
if len (as ) > 1 { //Slice has a head and a tail.
6
6
h , t := as [0 ], as [1 :len (as )]
@@ -15,12 +15,12 @@ func FoldRight[T1, T2 any](as []T1, z T2, f func(T1, T2) T2) T2 {
15
15
type fTypeFoldl [T1 , T2 any ] func ([]T1 , T2 , func (T2 , T1 ) T2 ) (T2 , tTypeFoldl [T2 ])
16
16
type tTypeFoldl [T2 any ] func () (T2 , tTypeFoldl [T2 ])
17
17
18
- //This is a stack-safe, tail recursive, pure function that uses the trampoline technique to ensure that the runtime
19
- //does not face recursion that is too deep(i.e. The garbage collector will run before the recursion gets deep enough to blow the stack).
20
- //See https://trinetri.wordpress.com/2015/04/28/tail-call-thunks-and-trampoline-in-golang/
21
- //A tail call is a function call that is the last action performed in a function.
22
- //The efficiency of this algorithm is O(N) and it does not reverse the list like FoldRight does.
23
- //Most of the other array functions in this package use FoldLeft and thus all are stack-safe.
18
+ // This is a stack-safe, tail recursive, pure function that uses the trampoline technique to ensure that the runtime
19
+ // does not face recursion that is too deep(i.e. The garbage collector will run before the recursion gets deep enough to blow the stack).
20
+ // See https://trinetri.wordpress.com/2015/04/28/tail-call-thunks-and-trampoline-in-golang/
21
+ // A tail call is a function call that is the last action performed in a function.
22
+ // The efficiency of this algorithm is O(N) and it does not reverse the list like FoldRight does.
23
+ // Most of the other array functions in this package use FoldLeft and thus all are stack-safe.
24
24
func FoldLeft [T1 , T2 any ](as []T1 , z T2 , f func (T2 , T1 ) T2 ) T2 {
25
25
accum , p := foldL (as , z , f )
26
26
for {
@@ -48,7 +48,7 @@ func foldL[T1, T2 any](as []T1, z T2, f func(T2, T1) T2) (T2, tTypeFoldl[T2]) {
48
48
h := as [0 ]
49
49
zz := f (z , h )
50
50
return zz , thunk (foldL [T1 , T2 ], Zero [T1 ](), zz , f )
51
- } else { //Causes the stack-safe funtion FoldLeft that uses the trampoilne technique to leave the for loop with the final accum result
51
+ } else { //Causes the stack-safe function FoldLeft that uses the trampoline technique to leave the recursion with the final accum result
52
52
return z , nil
53
53
}
54
54
}
@@ -62,14 +62,14 @@ func Zero[T any]() []T {
62
62
return []T {}
63
63
}
64
64
65
- //The efficiency of this algorithm is O(N)
65
+ // The efficiency of this algorithm is O(N)
66
66
func Reverse [T1 any ](xs []T1 ) []T1 {
67
67
f := Appender [T1 ]
68
68
return FoldRight (xs , Zero [T1 ](), f )
69
69
}
70
70
71
71
// A structure-preserving Functor on the given array of T.
72
- //The efficiency of this algorithm is O(N)
72
+ // The efficiency of this algorithm is O(N)
73
73
func Map [T1 , T2 any ](as []T1 , f func (T1 ) T2 ) []T2 {
74
74
g := func (as []T2 , s T1 ) []T2 {
75
75
gss := append (as , f (s ))
@@ -79,8 +79,8 @@ func Map[T1, T2 any](as []T1, f func(T1) T2) []T2 {
79
79
return xs
80
80
}
81
81
82
- //The efficiency of this algorithm is O(N)
83
- //Collapses the given array of arrays without changing the order.
82
+ // The efficiency of this algorithm is O(N)
83
+ // Collapses the given array of arrays without changing the order.
84
84
func Concat [A any ](l [][]A ) []A {
85
85
g := func (s []A , as []A ) []A {
86
86
gss := append (s , as ... )
@@ -91,12 +91,12 @@ func Concat[A any](l [][]A) []A {
91
91
92
92
// Similar to Map in that it takes an array of T1 and applies a function to each element.
93
93
// But FlatMap is more powerful than map. We can use flatMap to generate a collection that is either larger or smaller than the original input.
94
- //The efficiency of this algorithm is O(N squared)
94
+ // The efficiency of this algorithm is O(N squared)
95
95
func FlatMap [T1 , T2 any ](as []T1 , f func (T1 ) []T2 ) []T2 {
96
96
return Concat (Map (as , f ))
97
97
}
98
98
99
- //The efficiency of this algorithm is O(N)
99
+ // The efficiency of this algorithm is O(N)
100
100
func Filter [T any ](as []T , p func (T ) bool ) []T {
101
101
var g = func (accum []T , h T ) []T {
102
102
if p (h ) {
@@ -108,16 +108,16 @@ func Filter[T any](as []T, p func(T) bool) []T {
108
108
return FoldLeft (as , []T {}, g )
109
109
}
110
110
111
- //The efficiency of this algorithm is O(N)
112
- //Appends as2 to the end of as1
111
+ // The efficiency of this algorithm is O(N)
112
+ // Appends as2 to the end of as1
113
113
func Append [T any ](as1 , as2 []T ) []T {
114
114
var g = func (accum []T , h T ) []T {
115
115
return append (accum , h )
116
116
}
117
117
return FoldLeft (as2 , as1 , g )
118
118
}
119
119
120
- //The efficiency of this algorithm is O(N)
120
+ // The efficiency of this algorithm is O(N)
121
121
func Contains [T any ](source []T , contains T , equality func (l , r T ) bool ) bool {
122
122
p := func (s T ) bool {
123
123
if equality (s , contains ) {
@@ -134,7 +134,7 @@ func Contains[T any](source []T, contains T, equality func(l, r T) bool) bool {
134
134
}
135
135
}
136
136
137
- //The efficiency of this algorithm is O(N)
137
+ // The efficiency of this algorithm is O(N)
138
138
func ContainsAllOf [T any ](source []T , contains []T , equality func (l , r T ) bool ) bool {
139
139
for _ , v := range contains {
140
140
if ! Contains (source , v , equality ) {
@@ -144,7 +144,7 @@ func ContainsAllOf[T any](source []T, contains []T, equality func(l, r T) bool)
144
144
return true
145
145
}
146
146
147
- //The efficiency of this algorithm is O(N)
147
+ // The efficiency of this algorithm is O(N)
148
148
func ArrayEquality [T any ](aa []T , bb []T , equality func (l , r T ) bool ) bool {
149
149
f := func (aa , bb []T ) bool {
150
150
for i , _ := range aa {
0 commit comments