I'm learning Nim (previously used Python and C++/etc.) and trying to attempt https://adventofcode.com/2018/day/2.
I'm using a CountTable to count how many times each character appears:
for line in f.lines:
if line.len == 0: break
var cnts = CountTable[char]()
for c in line:
cnts.inc(c)
d2.nim(23) d2
tables.nim(1004) inc
system.nim(414) rawGet
system.nim(2830) sysFatal
Error: unhandled exception: index out of bounds [IndexError]
Version: kubuntu 18.04:
~ ❯❯❯ nim
Nim Compiler Version 0.19.0 [Linux: amd64]
Compiled at 2018-09-26
Copyright (c) 2006-2018 by Andreas Rumpf
The documentation for CountTable is autogenerated and lacking (no "description" explaining how to use CountTable).
This was improved during Hacktoberfest, but it has not yet hit the stable version. For the devel docs of tables module (including a short example how to use CountTable), see here.
The problem with your code is that you need to initialize your Tables. Use initCountTable[char]().
Thanks for the help, I'll fix my program if I get back to it.
I feel this issue is easy to hit, and near-impossible for me to resolve myself by looking at the docs, without forum help.
I looked at the docs:
Constructors don't even have a consistent language convention. See newSeq vs initCountTable
See the first two lines of the big table here: https://nim-lang.org/docs/nep1.html
As for other points you raise: Nim documentation really needs lots of improvements. Since you already burned yourself with the poor Tables documentation, how about creating a PR which will fix (some of) these issues you mention?
I assume my comment on Inc() is fairly noncontroversial.
Will moving the example code to the type definitions be accepted? Will moving the procs/methods below their corresponding type definitions be accepted?
Global example code can be done before the type definitions like here: https://github.com/nim-lang/Nim/blob/72e15ff739cc73fbf6e3090756d3f9cb3d5af2fa/lib/pure/collections/tables.nim#L100
Procs and methods are already below there type definitions, see here: https://github.com/nim-lang/Nim/blob/72e15ff739cc73fbf6e3090756d3f9cb3d5af2fa/lib/pure/collections/tables.nim#L872
Regarding your code you should init the countTable once and clear it in a loop. Otherwise you will put a lot of pressure on the GC and the memory allocator and memory allocation in a loop is very slow.
var cnts = CountTable[char]()
for line in f.lines:
if line.len == 0: break
cnts.clear()
for c in line:
cnts.inc(c)
Global example code can be done before the type definitions Procs and methods are already below there type definitions
In https://nim-lang.github.io/Nim/tables.html, all examples are on top regardless of type, followed by all type definitions (https://nim-lang.github.io/Nim/tables.html#7), followed by all procs regardless of type (https://nim-lang.github.io/Nim/tables.html#12).
I wanted each type should have its own <h1> with sub-headers for type definition, example usage, and procs (in some order).
(do you want me to stay and help, or am i being annoying?)
Unfortunately this is the doc generator that works like this.
But you can raise a feature request to give more visibility: https://github.com/nim-lang/Nim/issues (or change the title of this thread so that people can see the new subject at a glance).