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