diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2017-12-27 15:02:25 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-27 15:02:25 +0000 |
commit | eea08381d7277fdaa9be8c75f2a10fd9ce21d065 (patch) | |
tree | 09e6add7506543acad356e0dfcc57cf26e0e46df | |
parent | c19c6b1fae5640edcf1a522f526b786a3f0482e1 (diff) | |
parent | a8b0a8a92da03593c3fe52dfbca8ee4eeed286d3 (diff) | |
download | Nim-eea08381d7277fdaa9be8c75f2a10fd9ce21d065.tar.gz |
Merge pull request #6977 from moigagoo/issue-6976
Tables: make `toCountTable` actually count the elements of the input openArray.
-rw-r--r-- | changelog.md | 29 | ||||
-rw-r--r-- | lib/pure/collections/tables.nim | 5 |
2 files changed, 27 insertions, 7 deletions
diff --git a/changelog.md b/changelog.md index 5734a4cb1..de4b2f251 100644 --- a/changelog.md +++ b/changelog.md @@ -146,7 +146,7 @@ This now needs to be written as: - Asynchronous programming for the JavaScript backend using the `asyncjs` module. - Extra semantic checks for procs with noreturn pragma: return type is not allowed, statements after call to noreturn procs are no longer allowed. -- Noreturn proc calls and raising exceptions branches are now skipped during common type +- Noreturn proc calls and raising exceptions branches are now skipped during common type deduction in if and case expressions. The following code snippets now compile: ```nim import strutils @@ -159,10 +159,29 @@ let b = case str: of nil, "": raise newException(ValueError, "Invalid boolean") elif str.startsWith("Y"): true elif str.startsWith("N"): false - else: false -let c = if str == "Y": true - elif str == "N": false + else: false +let c = if str == "Y": true + elif str == "N": false else: - echo "invalid bool" + echo "invalid bool" quit("this is the end") ``` +- Proc [toCountTable](https://nim-lang.org/docs/tables.html#toCountTable,openArray[A]) now produces a `CountTable` with values correspoding to the number of occurrences of the key in the input. It used to produce a table with all values set to `1`. + +Counting occurrences in a sequence used to be: + +```nim +let mySeq = @[1, 2, 1, 3, 1, 4] +var myCounter = initCountTable[int]() + +for item in mySeq: + myCounter.inc item +``` + +Now, you can simply do: + +```nim +let + mySeq = @[1, 2, 1, 3, 1, 4] + myCounter = mySeq.toCountTable() +``` diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index 01767956e..777beabc3 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -966,9 +966,10 @@ proc initCountTable*[A](initialSize=64): CountTable[A] = newSeq(result.data, initialSize) proc toCountTable*[A](keys: openArray[A]): CountTable[A] = - ## creates a new count table with every key in `keys` having a count of 1. + ## creates a new count table with every key in `keys` having a count + ## of how many times it occurs in `keys`. result = initCountTable[A](rightSize(keys.len)) - for key in items(keys): result[key] = 1 + for key in items(keys): result.inc key proc `$`*[A](t: CountTable[A]): string = ## The `$` operator for count tables. |