-
Notifications
You must be signed in to change notification settings - Fork 0
/
minmax.nim
290 lines (251 loc) · 8.38 KB
/
minmax.nim
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# https://forum.nim-lang.org/t/4763
func maxIndexBy*[T](s: openArray[T], cmp: proc (x, y: T): int {.closure, noSideEffect.}): int =
## Returns the index of the maximum value of all elements in s.
## Comparison is done by cmp() proc.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].maxIndexBy(proc (x, y: string): int = cmp(x.len, y.len)) == 1
if s.len > 0:
var maxVal = s[0]
for i in 1 .. s.high:
if cmp(s[i], maxVal) > 0:
result = i
maxVal = s[i]
func minIndexBy*[T](s: openArray[T], cmp: proc (x, y: T): int {.closure, noSideEffect.}): int =
## Returns the index of the minimum value of all elements in s.
## Comparison is done by cmp() proc.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].minIndexBy(proc (x, y: string): int = cmp(x.len, y.len)) == 3
if s.len > 0:
var minVal = s[0]
for i in 1 .. s.high:
if cmp(s[i], minVal) < 0:
result = i
minVal = s[i]
# see system.sortedByIt()
template maxIndexByIt*(seq1, op: untyped): untyped =
## Returns the maximum index of all elements in s.
## Comparison is done by it expression
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].maxIndexByIt(it.len) == 1
var result = maxIndexBy(seq1, proc(x, y: type(seq1[0])): int =
var it {.inject.} = x
let a = op
it = y
let b = op
result = cmp(a, b))
result
template minIndexByIt*(seq1, op: untyped): untyped =
## Returns the minimum index of all elements in s.
## Comparison is done by it expression.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].minIndexByIt(it.len) == 3
var result = minIndexBy(seq1, proc(x, y: type(seq1[0])): int =
var it {.inject.} = x
let a = op
it = y
let b = op
result = cmp(a, b))
result
template maxIndex*(seq1, el, op: untyped): untyped =
## Returns the maximum index of all elements in s.
## Comparison is done by custom expression.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].maxIndex(el, el.len) == 1
var result = maxIndexBy(seq1, proc(x, y: type(seq1[0])): int =
var el {.inject.} = x
let a = op
el = y
let b = op
result = cmp(a, b))
result
template minIndex*(seq1, el, op: untyped): untyped =
## Returns the minimum index of all elements in s.
## Comparison is done by custom expression.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].minIndex(el, el.len) == 3
var result = minIndexBy(seq1, proc(x, y: type(seq1[0])): int =
var el {.inject.} = x
let a = op
el = y
let b = op
result = cmp(a, b))
result
func maxValueBy*[T](s: openArray[T], cmp: proc (x, y: T): int {.closure, noSideEffect.}): T =
## Returns the maximum value of all elements in s.
## Comparison is done by cmp() proc.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].maxValueBy(proc (x, y: string): int = cmp(x.len, y.len)) == "Nim"
assert(s.len > 0)
result = s[0]
for i in 1 .. s.high:
if cmp(s[i], result) > 0:
result = s[i]
func minValueBy*[T](s: openArray[T], cmp: proc (x, y: T): int {.closure, noSideEffect.}): T =
## Returns the minimum value of all elements in s.
## Comparison is done by cmp() proc.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].minValueBy(proc (x, y: string): int = cmp(x.len, y.len)) == "D"
assert(s.len > 0)
result = s[0]
for i in 1 .. s.high:
if cmp(s[i], result) < 0:
result = s[i]
template maxValueByIt*(seq1, op: untyped): untyped =
## Returns the maximum value of all elements in s.
## Comparison is done by it expression.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].maxValueByIt(it.len) == "Nim"
var result = maxValueBy(seq1, proc(x, y: type(seq1[0])): int =
var it {.inject.} = x
let a = op
it = y
let b = op
result = cmp(a, b))
result
template minValueByIt*(seq1, op: untyped): untyped =
## Returns the minimum value of all elements in s.
## Comparison is done by it expression.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].minValueByIt(it.len) == "D"
var result = minValueBy(seq1, proc(x, y: type(seq1[0])): int =
var it {.inject.} = x
let a = op
it = y
let b = op
result = cmp(a, b))
result
template maxValue*(seq1, el, op: untyped): untyped =
## Returns the maximum value of all elements in s.
## Comparison is done by custom expression.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].maxValue(el, el.len) == "Nim"
var result = maxValueBy(seq1, proc(x, y: type(seq1[0])): int =
var el {.inject.} = x
let a = op
el = y
let b = op
result = cmp(a, b))
result
template minValue*(seq1, el, op: untyped): untyped =
## Returns the minimum value of all elements in s.
## Comparison is done by custom expression.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].minValue(el, el.len) == "D"
var result = minValueBy(seq1, proc(x, y: type(seq1[0])): int =
var el {.inject.} = x
let a = op
el = y
let b = op
result = cmp(a, b))
result
func minMaxValueBy*[T](s: openArray[T], cmp: proc (x, y: T): int {.closure, noSideEffect.}): (T, T) =
## Returns the minimum and maximum values of all elements in s.
## Comparison is done by cmp() proc.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].minMaxValueBy(proc (x, y: string): int = cmp(x.len, y.len)) == ("D", "Nim")
assert(s.len > 0)
if s.len > 0:
result[0] = s[0]
result[1] = s[0]
for i in 1 .. s.high:
if cmp(result[1], s[i]) < 0:
result[1] = s[i]
elif cmp(result[0], s[i]) > 0:
result[0] = s[i]
template minMaxValueByIt*(seq1, op: untyped): untyped =
## Returns the minimum and maximum values of all elements in s.
## Comparison is done by it expression.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].minMaxValueByIt(it.len) == ("D", "Nim")
var result = minMaxValueBy(seq1, proc(x, y: type(seq1[0])): int =
var it {.inject.} = x
let a = op
it = y
let b = op
result = cmp(a, b))
result
template minMaxValue*(seq1, el, op: untyped): untyped =
## Returns the minimum and maximum values of all elements in s.
## Comparison is done by custom expression.
##
## Example:
##
## .. code-block:
## assert ["Go", "Nim", "C++", "D"].minMaxValue(el, el.len) == ("D", "Nim")
var result = minMaxValueBy(seq1, proc(x, y: type(seq1[0])): int =
var el {.inject.} = x
let a = op
el = y
let b = op
result = cmp(a, b))
result
when isMainModule:
type
Person = object
name: string
age: int
var people: seq[Person] = @[Person(name: "Eve", age: 11), Person(name: "Sarah", age: 25), Person(name: "Anna", age: 23)]
var (min, max) = people.minMaxValue(p, p.name)
echo "Min: ", min.name, ", ", min.age
echo "Max: ", max.name, ", ", max.age
var m = people.maxValueByIt(it.name.len)
echo "Max by length of name: ", m.name
var h = people.minIndexBy(proc (x, y: Person): int = cmp(x.name.len, y.name.len))
echo "Min index by length of name: ", h
assert ["Go", "Nim", "C++", "D"].maxIndexBy(proc (x, y: string): int = cmp(x.len, y.len)) == 1
assert ["Go", "Nim", "C++", "D"].minIndexBy(proc (x, y: string): int = cmp(x.len, y.len)) == 3
assert ["Go", "Nim", "C++", "D"].maxIndexByIt(it.len) == 1
assert ["Go", "Nim", "C++", "D"].minIndexByIt(it.len) == 3
assert ["Go", "Nim", "C++", "D"].maxIndex(el, el.len) == 1
assert ["Go", "Nim", "C++", "D"].minIndex(el, el.len) == 3
assert ["Go", "Nim", "C++", "D"].maxValueBy(proc (x, y: string): int = cmp(x.len, y.len)) == "Nim"
assert ["Go", "Nim", "C++", "D"].minValueBy(proc (x, y: string): int = cmp(x.len, y.len)) == "D"
assert ["Go", "Nim", "C++", "D"].maxValueByIt(it.len) == "Nim"
assert ["Go", "Nim", "C++", "D"].minValueByIt(it.len) == "D"
assert ["Go", "Nim", "C++", "D"].maxValue(el, el.len) == "Nim"
assert ["Go", "Nim", "C++", "D"].minValue(el, el.len) == "D"
assert ["Go", "Nim", "C++", "D"].minMaxValueBy(proc (x, y: string): int = cmp(x.len, y.len)) == ("D", "Nim")
assert ["Go", "Nim", "C++", "D"].minMaxValueByIt(it.len) == ("D", "Nim")
assert ["Go", "Nim", "C++", "D"].minMaxValue(el, el.len) == ("D", "Nim")