Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min/max overloads with comparison functions #23595

Open
wants to merge 21 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,22 @@ func minIndex*[T](s: openArray[T]): int {.since: (1, 1).} =
for i in 1..high(s):
if s[i] < s[result]: result = i

func minIndex*[T](s: openArray[T], cmp: proc(a, b: T): bool {.closure.}): int {.
since: (1, 1), effectsOf: cmp.} =
AmjadHD marked this conversation as resolved.
Show resolved Hide resolved
## Returns the index of the minimum value of `s`.
## `cmp` should return true if `a` is *less* than `b`.
runnableExamples:
import std/sugar

let s1 = @["foo","bar", "hello"]
let s2 = @[2..4, 1..3, 6..10]
assert minIndex(s1, proc (a, b: string): bool = a.len < b.len) == 0
assert minIndex(s2, (a, b) => a.a < b.a) == 1

for i in 1..high(s):
if cmp(s[i], s[result]): result = i


func maxIndex*[T](s: openArray[T]): int {.since: (1, 1).} =
## Returns the index of the maximum value of `s`.
## `T` needs to have a `<` operator.
Expand All @@ -265,6 +281,21 @@ func maxIndex*[T](s: openArray[T]): int {.since: (1, 1).} =
for i in 1..high(s):
if s[i] > s[result]: result = i

func maxIndex*[T](s: openArray[T], cmp: proc(a, b: T): bool {.closure.}): int {.
since: (1, 1), effectsOf: cmp.} =
AmjadHD marked this conversation as resolved.
Show resolved Hide resolved
## Returns the index of the maximum value of `s`.
## `cmp` should return true if `a` is *less* than `b`.
runnableExamples:
import std/sugar

let s1 = @["foo","bar", "hello"]
let s2 = @[2..4, 1..3, 6..10]
assert maxIndex(s1, proc (a, b: string): bool = a.len < b.len) == 2
assert maxIndex(s2, (a, b) => a.a < b.a) == 2

for i in 1..high(s):
if cmp(s[result], s[i]): result = i

func minmax*[T](x: openArray[T]): (T, T) =
## The minimum and maximum values of `x`. `T` needs to have a `<` operator.
var l = x[0]
Expand All @@ -274,6 +305,14 @@ func minmax*[T](x: openArray[T]): (T, T) =
if h < x[i]: h = x[i]
result = (l, h)

func minmax*[T](x: openArray[T], cmp: proc(a, b: T): bool {.closure.}): (T, T) {.effectsOf: cmp.} =
## The minimum and maximum values of `x`.
## `cmp` should return true if `a` is *less* than `b`.
result = (x[0], x[0])
for i in 1..high(x):
if cmp(x[i], result[0]): result[0] = x[i]
if cmp(result[1], x[i]): result[1] = x[i]


template zipImpl(s1, s2, retType: untyped): untyped =
proc zip*[S, T](s1: openArray[S], s2: openArray[T]): retType =
Expand Down
14 changes: 14 additions & 0 deletions lib/system/comparisons.nim
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,26 @@ proc min*[T](x: openArray[T]): T =
for i in 1..high(x):
if x[i] < result: result = x[i]

proc min*[T](x: openArray[T], cmp: proc(a, b: T): bool {.closure.}): T =
## The minimum value of `x`.
## `cmp` should return true if `a` is *less than* `b`.
result = x[0]
for i in 1..high(x):
if cmp(x[i], result): result = x[i]

proc max*[T](x: openArray[T]): T =
## The maximum value of `x`. `T` needs to have a `<` operator.
result = x[0]
for i in 1..high(x):
if result < x[i]: result = x[i]

proc max*[T](x: openArray[T], cmp: proc(a, b: T): bool {.closure.}): T {.effectsOf: cmp.} =
## The maximum value of `x`.
## `cmp` should return true if `a` is *less* than `b`.
result = x[0]
for i in 1..high(x):
if cmp(result, x[i]): result = x[i]

{.pop.} # stackTrace: off


Expand Down
Loading