-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListMultiply.go
116 lines (97 loc) · 2.49 KB
/
ListMultiply.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
package golist
import (
"github.com/emylincon/golist/core"
)
// ListMultiply returns the product of contents of two lists
func (arr *List) ListMultiply(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.ListMultiplyInt(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.ListMultiplyInt32(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.ListMultiplyInt64(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.ListMultiplyFloat32(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.ListMultiplyFloat64(list, otherArr)
return NewList(sum), nil
case []string:
return &List{}, ErrStringsNotsupported
default:
return &List{}, ErrTypeNotsupported
}
}
// ListMultiplyNo multiply a given number with all elements in list
func (arr *List) ListMultiplyNo(no interface{}) (*List, error) {
switch list := arr.list.(type) {
case []int:
o, ok := no.(int)
if !ok {
return &List{}, ErrTypeNotSame
}
sum := core.ListMultiplyNoInt(list, o)
return NewList(sum), nil
case []int32:
o, ok := no.(int32)
if !ok {
return &List{}, ErrTypeNotSame
}
sum := core.ListMultiplyNoInt32(list, o)
return NewList(sum), nil
case []int64:
o, ok := no.(int64)
if !ok {
return &List{}, ErrTypeNotSame
}
sum := core.ListMultiplyNoInt64(list, o)
return NewList(sum), nil
case []float32:
o, ok := no.(float32)
if !ok {
return &List{}, ErrTypeNotSame
}
sum := core.ListMultiplyNoFloat32(list, o)
return NewList(sum), nil
case []float64:
o, ok := no.(float64)
if !ok {
return &List{}, ErrTypeNotSame
}
sum := core.ListMultiplyNoFloat64(list, o)
return NewList(sum), nil
case []string:
return &List{}, ErrStringsNotsupported
default:
return &List{}, ErrTypeNotsupported
}
}