From 28fa1c3b40dec5e303e546255fe0ed113f2cdb05 Mon Sep 17 00:00:00 2001 From: def Date: Fri, 20 Feb 2015 04:57:53 +0100 Subject: [PATCH] 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.