-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfunctional_programming.go
More file actions
114 lines (108 loc) · 3.59 KB
/
functional_programming.go
File metadata and controls
114 lines (108 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package govector
//FpMap : Returns a copy of the vector that can be altered or not through the inline function
// _Parameters:
// function: T "A funciton with one arugument that matches the datatype of the vector's elements,
// it must return a value that is the same datatype as the slice values or it will crash the program"
// _Returns:
// T "The filtered vector that is the same datatype as the vector"
//
func (v *Vector) FpMap(function func(T) T) Vector {
var dataCopy Vector
dataCopy = dataCopy.Init()
for index := 0; index < len(v.data); index++ {
if function != nil {
mapFunc := function(v.data[index]) //v.Mapable(v.data[index])
dataCopy.PushBack(mapFunc)
} else {
dataCopy.PushBack(v.data[index])
}
}
return dataCopy
}
//FpReduce : reduces a whole value into one and treat it as a single sum from the vector
// _Parameters:
// function: T "A funciton with one arugument that matches the datatype of the vector's elements,
// it must return a value that is the same datatype as the slice values or it will crash the program
// _Returns:
// T "The accumulate value that is the same datatype as the vector"
//
func (v *Vector) FpReduce(function func(T, T) T) T {
sum := v.data[0]
for index := 1; index < len(v.data); index++ {
if function != nil {
sum = function(sum, v.data[index])
} else {
switch sum.(type) {
//For those of you wondering why there is a sum temp, it is because
//Go doesn't allow use to += with sum.(int), which is something we are trying to do
//THis seems like a little hack, but at least not a painful one....
case int:
sumTemp := sum.(int)
sumTemp += v.data[index].(int)
sum = sumTemp
break
case int16:
sumTemp := sum.(int16)
sumTemp += v.data[index].(int16)
sum = sumTemp
//The documentation said the int32 is not an alias for int
case int32:
sumTemp := sum.(int32)
sumTemp += v.data[index].(int32)
sum = sumTemp
case int64:
sumTemp := sum.(int64)
sumTemp += v.data[index].(int64)
sum = sumTemp
case float32:
sumTemp := sum.(float32)
sumTemp += v.data[index].(float32)
sum = sumTemp
case float64:
sumTemp := sum.(float64)
sumTemp += v.data[index].(float64)
sum = sumTemp
case string:
sumTemp := sum.(string)
sumTemp += v.data[index].(string)
sum = sumTemp
default:
panic(`Nil function failed to accumulate vector values, possible reasons \n
* Vector has mismatching types \n
* Attempting to add Types that don't have the + operator supported by default in Golang`)
}
}
}
return sum
}
//FpFilter : Returns a filtered copy of the vector that satifys the given conditions
// _Parameters:
// function: T "A funciton with one arugument that matches the datatype of the vector's elements,
// it must return a boolean or it will crash the program"
// _Returns:
// T "The filtered vector that is the same datatype as the vector"
//
func (v *Vector) FpFilter(function func(T) bool) Vector {
var dataCopy Vector
dataCopy = dataCopy.Init()
for index := 0; index < len(v.data); index++ {
if function(v.data[index]) {
dataCopy.PushBack(v.data[index])
}
}
return dataCopy
}
//FpIndexOf : returns the index of first occurance of the element if found, ortherwise it returns the size of the vecotor
// _Parameters:
// elementToFind: T "The element that is desired to be found"
// _Return :
// The index of the desired element
func (v *Vector) FpIndexOf(function func(T) bool) int {
for index := 0; index < v.Size()-1; index++ {
if function(v.data[index]) {
return index
}
}
// This is like saying "last" from the normal std::find
return v.Size()
}