From 28fa1c3b40dec5e303e546255fe0ed113f2cdb05 Mon Sep 17 00:00:00 2001 From: def Date: Fri, 20 Feb 2015 04:57:53 +0100 Subject: [PATCH 1/4] Add sorted proc to algorithm module --- lib/pure/algorithm.nim | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index 20bfc5c7c7..dbb133e30b 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -187,6 +187,13 @@ proc sort*[T](a: var openArray[T], dec(m, s*2) s = s*2 +proc sorted*[T](a: openArray[T], cmp: proc(x, y: T): int {.closure.}, order = SortOrder.Ascending): seq[T] = + ## returns `a` sorted by `cmp` in the specified `order`. + result = newSeq[T](a.len) + for i in 0 .. a.high: + result[i] = a[i] + sort(result, cmp, order) + proc product*[T](x: openArray[seq[T]]): seq[seq[T]] = ## produces the Cartesian product of the array. Warning: complexity ## may explode. From d3946aa6213f97dad994f716565161caf0be7806 Mon Sep 17 00:00:00 2001 From: def Date: Fri, 20 Feb 2015 05:01:29 +0100 Subject: [PATCH 2/4] Add sortedBy template to sequtils --- lib/pure/collections/sequtils.nim | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index e690e8ebaa..358e59fb13 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: ## From 2d879b96dfd59b8d27283d9267b168569c84ac0c Mon Sep 17 00:00:00 2001 From: def Date: Sat, 21 Feb 2015 12:30:06 +0100 Subject: [PATCH 3/4] Rename sortedBy to sortByIt --- lib/pure/collections/sequtils.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 358e59fb13..82088044cf 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -417,7 +417,7 @@ template mapIt*(varSeq, op: expr) = let it {.inject.} = varSeq[i] varSeq[i] = op -template sortedBy*(seq1, op: expr): expr = +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 From c3f6c7f8a3f908b9cf17b8140d21656bc2b5d8b0 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 3 Mar 2015 21:44:27 +0100 Subject: [PATCH 4/4] Move sortByIt to algorithm module and add an example for it --- lib/pure/algorithm.nim | 21 +++++++++++++++++++++ lib/pure/collections/sequtils.nim | 15 --------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index dbb133e30b..20976e7885 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -194,6 +194,27 @@ proc sorted*[T](a: openArray[T], cmp: proc(x, y: T): int {.closure.}, order = So result[i] = a[i] 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]] = ## produces the Cartesian product of the array. Warning: complexity ## may explode. diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 82088044cf..e690e8ebaa 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -26,8 +26,6 @@ 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. ## @@ -417,19 +415,6 @@ template mapIt*(varSeq, op: expr) = let it {.inject.} = varSeq[i] 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 = ## creates a new sequence, calling `init` to initialize each value. Example: ##