1package funk
2
3import "strings"
4
5// MaxInt validates the input, compares the elements and returns the maximum element in an array/slice.
6// It accepts []int
7// It returns int or nil
8// It returns nil for the following cases:
9//  - input is of length 0
10func MaxInt(i []int) interface{} {
11	if len(i) == 0 {
12		return nil
13	}
14	var max int
15	for idx := 0; idx < len(i); idx++ {
16		item := i[idx]
17		if idx == 0 {
18			max = item
19			continue
20		}
21		if item > max {
22			max = item
23		}
24	}
25	return max
26}
27
28// MaxInt8 validates the input, compares the elements and returns the maximum element in an array/slice.
29// It accepts []int8
30// It returns int8 or nil
31// It returns nil for the following cases:
32//  - input is of length 0
33func MaxInt8(i []int8) interface{} {
34	if len(i) == 0 {
35		return nil
36	}
37	var max int8
38	for idx := 0; idx < len(i); idx++ {
39		item := i[idx]
40		if idx == 0 {
41			max = item
42			continue
43		}
44		if item > max {
45			max = item
46		}
47	}
48	return max
49}
50
51// MaxInt16 validates the input, compares the elements and returns the maximum element in an array/slice.
52// It accepts []int16
53// It returns int16 or nil
54// It returns nil for the following cases:
55//  - input is of length 0
56func MaxInt16(i []int16) interface{} {
57	if len(i) == 0 {
58		return nil
59	}
60	var max int16
61	for idx := 0; idx < len(i); idx++ {
62		item := i[idx]
63		if idx == 0 {
64			max = item
65			continue
66		}
67		if item > max {
68			max = item
69		}
70	}
71	return max
72}
73
74// MaxInt32 validates the input, compares the elements and returns the maximum element in an array/slice.
75// It accepts []int32
76// It returns int32 or nil
77// It returns nil for the following cases:
78//  - input is of length 0
79func MaxInt32(i []int32) interface{} {
80	if len(i) == 0 {
81		return nil
82	}
83	var max int32
84	for idx := 0; idx < len(i); idx++ {
85		item := i[idx]
86		if idx == 0 {
87			max = item
88			continue
89		}
90		if item > max {
91			max = item
92		}
93	}
94	return max
95}
96
97// MaxInt64 validates the input, compares the elements and returns the maximum element in an array/slice.
98// It accepts []int64
99// It returns int64 or nil
100// It returns nil for the following cases:
101//  - input is of length 0
102func MaxInt64(i []int64) interface{} {
103	if len(i) == 0 {
104		return nil
105	}
106	var max int64
107	for idx := 0; idx < len(i); idx++ {
108		item := i[idx]
109		if idx == 0 {
110			max = item
111			continue
112		}
113		if item > max {
114			max = item
115		}
116	}
117	return max
118}
119
120// MaxFloat32 validates the input, compares the elements and returns the maximum element in an array/slice.
121// It accepts []float32
122// It returns float32 or nil
123// It returns nil for the following cases:
124//  - input is of length 0
125func MaxFloat32(i []float32) interface{} {
126	if len(i) == 0 {
127		return nil
128	}
129	var max float32
130	for idx := 0; idx < len(i); idx++ {
131		item := i[idx]
132		if idx == 0 {
133			max = item
134			continue
135		}
136		if item > max {
137			max = item
138		}
139	}
140	return max
141}
142
143// MaxFloat64 validates the input, compares the elements and returns the maximum element in an array/slice.
144// It accepts []float64
145// It returns float64 or nil
146// It returns nil for the following cases:
147//  - input is of length 0
148func MaxFloat64(i []float64) interface{} {
149	if len(i) == 0 {
150		return nil
151	}
152	var max float64
153	for idx := 0; idx < len(i); idx++ {
154		item := i[idx]
155		if idx == 0 {
156			max = item
157			continue
158		}
159		if item > max {
160			max = item
161		}
162	}
163	return max
164}
165
166// MaxString validates the input, compares the elements and returns the maximum element in an array/slice.
167// It accepts []string
168// It returns string or nil
169// It returns nil for the following cases:
170//  - input is of length 0
171func MaxString(i []string) interface{} {
172	if len(i) == 0 {
173		return nil
174	}
175	var max string
176	for idx := 0; idx < len(i); idx++ {
177		item := i[idx]
178		if idx == 0 {
179			max = item
180			continue
181		}
182		max = compareStringsMax(max, item)
183	}
184	return max
185}
186
187// compareStrings uses the strings.Compare method to compare two strings, and returns the greater one.
188func compareStringsMax(max, current string) string {
189	r := strings.Compare(strings.ToLower(max), strings.ToLower(current))
190	if r > 0 {
191		return max
192	}
193	return current
194}