diff options
author | def <dennis@felsin9.de> | 2015-02-20 05:01:29 +0100 |
---|---|---|
committer | def <dennis@felsin9.de> | 2015-02-20 05:01:29 +0100 |
commit | d3946aa6213f97dad994f716565161caf0be7806 (patch) | |
tree | 67241ee67bcc8c4ad411d6043e2001ac275f2d96 | |
parent | 28fa1c3b40dec5e303e546255fe0ed113f2cdb05 (diff) | |
download | Nim-d3946aa6213f97dad994f716565161caf0be7806.tar.gz |
Add sortedBy template to sequtils
-rw-r--r-- | lib/pure/collections/sequtils.nim | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index e690e8eba..358e59fb1 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -26,6 +26,8 @@ when not defined(nimhygiene): {.pragma: dirty.} +import algorithm + proc concat*[T](seqs: varargs[seq[T]]): seq[T] = ## Takes several sequences' items and returns them inside a new sequence. ## @@ -415,6 +417,19 @@ template mapIt*(varSeq, op: expr) = let it {.inject.} = varSeq[i] varSeq[i] = op +template sortedBy*(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 = ## creates a new sequence, calling `init` to initialize each value. Example: ## |