From 5498415f3b44048739c9b7116638824713d9c1df Mon Sep 17 00:00:00 2001 From: Simon Hafner Date: Thu, 6 Feb 2014 16:11:55 -0600 Subject: indexBy, which indexes a collection into a hashtable --- lib/pure/collections/tables.nim | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib/pure/collections/tables.nim') diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index 73da274b9..40ae57b5a 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -189,6 +189,16 @@ template dollarImpl(): stmt {.dirty.} = proc `$`*[A, B](t: TTable[A, B]): string = ## The `$` operator for hash tables. dollarImpl() + +proc `==`*[A, B](s, t: TTable[A, B]): bool = + s.counter == t.counter and s.data == t.data + +proc indexBy*[A, B, C](collection: A, index: proc(x: B): C): TTable[C, B] = + ## Index the collection with the proc provided. + # TODO: As soon as supported, change collection: A to collection: A[B] + result = initTable[C, B]() + for item in collection: + result[index(item)] = item # ------------------------------ ordered table ------------------------------ -- cgit 1.4.1-2-gfad0