-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListSum.go
127 lines (107 loc) · 2.61 KB
/
ListSum.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
package golist
import (
"github.com/emylincon/golist/core"
)
// validateListOp validates
func validateListOp(ok bool, listLen, otherLen int) error {
if !ok {
return ErrListsNotOfSameType
}
if listLen != otherLen {
return ErrListsNotOfSameLen
}
return nil
}
// ListSum returns the sum of contents of two lists
func (arr *List) ListSum(other *List) (*List, error) {
switch list := arr.list.(type) {
case []int:
otherArr, ok := other.list.([]int)
err := validateListOp(ok, arr.Len(), other.Len())
if err != nil {
return &List{}, err
}
sum := core.ListSumInt(list, otherArr)
return NewList(sum), nil
case []int32:
otherArr, ok := other.list.([]int32)
err := validateListOp(ok, arr.Len(), other.Len())
if err != nil {
return &List{}, err
}
sum := core.ListSumInt32(list, otherArr)
return NewList(sum), nil
case []int64:
otherArr, ok := other.list.([]int64)
err := validateListOp(ok, arr.Len(), other.Len())
if err != nil {
return &List{}, err
}
sum := core.ListSumInt64(list, otherArr)
return NewList(sum), nil
case []float32:
otherArr, ok := other.list.([]float32)
err := validateListOp(ok, arr.Len(), other.Len())
if err != nil {
return &List{}, err
}
sum := core.ListSumFloat32(list, otherArr)
return NewList(sum), nil
case []float64:
otherArr, ok := other.list.([]float64)
err := validateListOp(ok, arr.Len(), other.Len())
if err != nil {
return &List{}, err
}
sum := core.ListSumFloat64(list, otherArr)
return NewList(sum), nil
case []string:
return &List{}, ErrStringsNotsupported
default:
return &List{}, ErrTypeNotsupported
}
}
// ListSumNo adds a given number to all elements in list
func (arr *List) ListSumNo(no interface{}) (*List, error) {
switch list := arr.list.(type) {
case []int:
o, ok := no.(int)
if !ok {
return &List{}, ErrTypeNotSame
}
sum := core.ListSumNoInt(list, o)
return NewList(sum), nil
case []int32:
o, ok := no.(int32)
if !ok {
return &List{}, ErrTypeNotSame
}
sum := core.ListSumNoInt32(list, o)
return NewList(sum), nil
case []int64:
o, ok := no.(int64)
if !ok {
return &List{}, ErrTypeNotSame
}
sum := core.ListSumNoInt64(list, o)
return NewList(sum), nil
case []float32:
o, ok := no.(float32)
if !ok {
return &List{}, ErrTypeNotSame
}
sum := core.ListSumNoFloat32(list, o)
return NewList(sum), nil
case []float64:
o, ok := no.(float64)
if !ok {
return &List{}, ErrTypeNotSame
}
sum := core.ListSumNoFloat64(list, o)
return NewList(sum), nil
case []string:
return &List{}, ErrStringsNotsupported
default:
return &List{}, ErrTypeNotsupported
}
}