Move sortByIt to algorithm module and add an example for it
This commit is contained in:
parent
2d879b96df
commit
c3f6c7f8a3
2 changed files with 21 additions and 15 deletions
|
|
@ -194,6 +194,27 @@ proc sorted*[T](a: openArray[T], cmp: proc(x, y: T): int {.closure.}, order = So
|
||||||
result[i] = a[i]
|
result[i] = a[i]
|
||||||
sort(result, cmp, order)
|
sort(result, cmp, order)
|
||||||
|
|
||||||
|
template sortByIt*(seq1, op: expr): expr =
|
||||||
|
## Convenience template around the ``sorted`` proc to reduce typing.
|
||||||
|
##
|
||||||
|
## The template injects the ``it`` variable which you can use directly in an
|
||||||
|
## expression. Example:
|
||||||
|
##
|
||||||
|
## .. code-block:: nim
|
||||||
|
##
|
||||||
|
## var users: seq[tuple[id: int, name: string]] =
|
||||||
|
## @[(0, "Smith"), (1, "Pratt"), (2, "Sparrow")]
|
||||||
|
##
|
||||||
|
## echo users.sortByIt(it.name)
|
||||||
|
##
|
||||||
|
var result {.gensym.} = sorted(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
|
||||||
|
|
||||||
proc product*[T](x: openArray[seq[T]]): seq[seq[T]] =
|
proc product*[T](x: openArray[seq[T]]): seq[seq[T]] =
|
||||||
## produces the Cartesian product of the array. Warning: complexity
|
## produces the Cartesian product of the array. Warning: complexity
|
||||||
## may explode.
|
## may explode.
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,6 @@
|
||||||
when not defined(nimhygiene):
|
when not defined(nimhygiene):
|
||||||
{.pragma: dirty.}
|
{.pragma: dirty.}
|
||||||
|
|
||||||
import algorithm
|
|
||||||
|
|
||||||
proc concat*[T](seqs: varargs[seq[T]]): seq[T] =
|
proc concat*[T](seqs: varargs[seq[T]]): seq[T] =
|
||||||
## Takes several sequences' items and returns them inside a new sequence.
|
## Takes several sequences' items and returns them inside a new sequence.
|
||||||
##
|
##
|
||||||
|
|
@ -417,19 +415,6 @@ template mapIt*(varSeq, op: expr) =
|
||||||
let it {.inject.} = varSeq[i]
|
let it {.inject.} = varSeq[i]
|
||||||
varSeq[i] = op
|
varSeq[i] = op
|
||||||
|
|
||||||
template sortByIt*(seq1, op: expr): expr =
|
|
||||||
## Convenience template around the ``sorted`` proc to reduce typing.
|
|
||||||
##
|
|
||||||
## The template injects the ``it`` variable which you can use directly in an
|
|
||||||
## expression.
|
|
||||||
var result {.gensym.} = sorted(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 newSeqWith*(len: int, init: expr): expr =
|
template newSeqWith*(len: int, init: expr): expr =
|
||||||
## creates a new sequence, calling `init` to initialize each value. Example:
|
## creates a new sequence, calling `init` to initialize each value. Example:
|
||||||
##
|
##
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue