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

Idxmin & idxmax, continuation #13208

Merged
merged 11 commits into from
Jan 20, 2020
8 changes: 1 addition & 7 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,21 @@
- introduced new procs in `tables.nim`: `OrderedTable.pop`, `CountTable.del`,
`CountTable.pop`, `Table.pop`
- To `strtabs.nim`, added `StringTable.clear` overload that reuses the existing mode.


- Added `sugar.outplace` for turning in-place algorithms like `sort` and `shuffle` into
operations that work on a copy of the data and return the mutated copy. As the existing
`sorted` does.
- Added `sugar.collect` that does comprehension for seq/set/table collections.

- Added `sugar.capture` for capturing some local loop variables when creating a closure.
This is an enhanced version of `closureScope`.

- Added `typetraits.lenTuple` to get number of elements of a tuple/type tuple,
and `typetraits.get` to get the ith element of a type tuple.
- Added `typetraits.genericParams` to return a tuple of generic params from a generic instantiation

- Added `os.normalizePathEnd` for additional path sanitization.

- Added `times.fromUnixFloat,toUnixFloat`, subsecond resolution versions of `fromUnix`,`toUnixFloat`.

- Added `wrapnils` module for chains of field-access and indexing where the LHS can be nil.
This simplifies code by reducing need for if-else branches around intermediate maybe nil values.
Eg: `echo ?.n.typ.kind`
- Added `minIndex` and `maxIndex` to the `sequtils` module

## Library changes

Expand Down
35 changes: 35 additions & 0 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,41 @@ proc deduplicate*[T](s: openArray[T], isSorted: bool = false): seq[T] =
for itm in items(s):
if not result.contains(itm): result.add(itm)

proc minIndex*[T](s: openArray[T]): int {.since: (1, 1).} =
## Returns the index of the minimum value of `s`.
Copy link
Member

@timotheecour timotheecour Jan 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • should make it clear what happens in case of duplicate such indexes (the index is incorrect / imprecise); eg: In case of duplicates, the smallest such index is returned
  • arguable, but IMO returning -1 for s.len == 0 may make more sense
  • all (ok almost all) procs here return seqs; but not minIndex, maxIndex; IMO this would make more sense in std/algorithm
  • this could take an optional cmp argument, like other procs in std/algorithm
  • nit: isn't argMin, argMax more common?

## ``T`` needs to have a ``<`` operator.
runnableExamples:
let
a = @[1, 2, 3, 4]
b = @[6, 5, 4, 3]
c = [2, -7, 8, -5]
d = "ziggy"
assert minIndex(a) == 0
assert minIndex(b) == 3
assert minIndex(c) == 1
assert minIndex(d) == 2

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

proc maxIndex*[T](s: openArray[T]): int {.since: (1, 1).} =
## Returns the index of the maximum value of `s`.
## ``T`` needs to have a ``<`` operator.
runnableExamples:
let
a = @[1, 2, 3, 4]
b = @[6, 5, 4, 3]
c = [2, -7, 8, -5]
d = "ziggy"
assert maxIndex(a) == 3
assert maxIndex(b) == 0
assert maxIndex(c) == 2
assert maxIndex(d) == 0

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


template zipImpl(s1, s2, retType: untyped): untyped =
proc zip*[S, T](s1: openArray[S], s2: openArray[T]): retType =
## Returns a new sequence with a combination of the two input containers.
Expand Down