Nested Tables

2021-11-05 Thread SunnyCorleone
I dont mind the default value behaviour, although obviously it shouldnt happen when looking up a key or checking the length.

Nested Tables

2021-11-04 Thread dxb
I created as a starting point for the discussion.

Nested Tables

2021-11-03 Thread ElegantBeef
I do not think there is an issue, probably best that one is made, this issue is a fickle one that probably requires conversation as there are two possible venues on when to borrow. You can borrow at proc definition or at proc instantiation, which both have benefits/downsides. Presently I'm leani

Nested Tables

2021-11-03 Thread shirleyquirk
Here's a really rough sketch of a DefaultTable that follows the python API of letting you define a lambda for the default value. along with a NestedTable alias. There is a _[lot](https://forum.nim-lang.org/postActivity.x

Nested Tables

2021-11-03 Thread dxb
Hi @ElegantBeef, is there an issue number for tracking this? Thank you!

Nested Tables

2021-11-01 Thread PMunch
I'd still go with the one @shirleyquirk posted. That version will only do it for tables, and not arbitrary values, saving you from the issue that the C++ code had. Compare this: proc `[]`[A, B, C](t: var Table[A, Table[B, C]], key: A): var Table[B, C] = t.mgetOrPut(key, initTa

Nested Tables

2021-11-01 Thread Araq
> One of the things I like about Nim is how it uses terms properly. I also considered to use `text` instead of `string` and `chain` instead of (linked) `list`. ;-)

Nested Tables

2021-10-31 Thread ElegantBeef
Borrowing has some edge cases where it cannot find the symbols, I'll have to look into this, since I thought I fixed this.

Nested Tables

2021-10-31 Thread dxb
N00b here. I tried creating a `distinct DefaultTable` type but I cannot borrow `mgetOrPut`. Is this code supposed to compile? What am I missing? import tables type DefaultTable[A, B] = distinct Table[A, B] proc mgetOrPut[A

Nested Tables

2021-10-31 Thread xigoi
One of the things I like about Nim is how it uses terms properly. * `seq`, not `vector` * proper distinction between `proc`, `func`, `method` * `const` is actually constant * `Table`, not `Dict` or `Map`

Nested Tables

2021-10-31 Thread SunnyCorleone
> Depends on the context, when I wrote an RTS I had "maps" everywhere and it > wasn't a hash table. Likewise when I write a spell checker the last word I > want to have in my stdlib is "dict". These are very specific instances though, and not sure how keywords Map or Dict would really get in yo

Nested Tables

2021-10-31 Thread shirleyquirk
oops sorry, yes, typo. edited my post > I just don't like copy and pasting code that I don't understand into my > projects absolutely! if you don't understand it, you don't know what you can change without breaking things! for example, the reason @Hlaaftana didn't use `[]`, but instead `()`, w

Nested Tables

2021-10-31 Thread Araq
> When you think of table you think of html table. Depends on the context, when I wrote an RTS I had "maps" everywhere and it wasn't a hash table. Likewise when I write a spell checker the last word I want to have in my stdlib is "dict".

Nested Tables

2021-10-30 Thread SunnyCorleone
I wasn't doubting that it worked, I just don't like copy and pasting code that I don't understand into my projects, thats what bootcamp web developers do. Id prefer that operator overloading and generic magic to be hidden from me in the std lib and learn to use it in my own time. I think your t

Nested Tables

2021-10-30 Thread Fire
You do not need to write a library for the data-structure. A wrapper of its API would be sufficient for this purpose, I believe. Also, I googled"nim dictionary". The fourth result is a forum post, fifth std/tables. Not ideal, I agree, but that is what happens when you share your name with a some

Nested Tables

2021-10-30 Thread SunnyCorleone
> Why? It is a HashTable. I think it makes perfect sense. If you are really > bothered with the Nested Tables thing, then you might make an RFC for them, > or make your own quick library. Its a hash table but widely referred to as map or dictionary, if you checked google searches I

Nested Tables

2021-10-30 Thread shirleyquirk
@Hlaaftana's answer looks like exactly what you're after, i think you should take another look: import tables proc `[]`[A,B,C](t:var Table[A,Table[B,C]],key:A):var Table[B,C] = t.mgetOrPut(key,initTable[B,C]) var nested:Table[string,Table[string,Table[str

Nested Tables

2021-10-30 Thread stbalbach
Yes wish tables were more intuitive. Maybe the correct term is associative array. It can sort of made easy with some init setup. This for a two key table, same idea for any table size or type type TwoKeyTable* = Table[string, Table[string, string]] proc initTwoKeyTabl

Nested Tables

2021-10-30 Thread alexeypetrushin
I miss this feature too, it's very convenient (Ruby version) h = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = Hash.new } } h[:a][:b][:c] = 1 p h # => { a: { b: { c: 1 } } } Run

Nested Tables

2021-10-30 Thread doofenstein
the downside to this is that in C++ this code: ++ #include #include int main() { std::map test; test[42]; printf("test %d\n", test.size()); } Run prints 1. Eventhough there's no visible assignment at any point a new tab

Nested Tables

2021-10-30 Thread Fire
Why? It is a HashTable. I think it makes perfect sense.

Nested Tables

2021-10-30 Thread SunnyCorleone
Initial impression of the Table module is disappointing, I dont think Nim does it in an intuitive or convenient way, its such an important data structure and it shouldnt be called Table anyway. Even in QT they call it QHash or QMap(ordered) probably because Table is associated with something com

Nested Tables

2021-10-30 Thread SunnyCorleone
I appreciate the detailed replies, I will spend some time to try and understand the code you have posted. But for Nim its not a good look when c++ makes something more simple. #include #include #include using namespace std; typedef map>>> NestedMap;

Nested Tables

2021-10-30 Thread Hlaaftana
`mgetOrPut` accesses a key, if the key doesn't exist then it is set to the value you give it, and returns the address to the value of that key. You can then modify the value in this address, either by setting a key inside it, or by calling `mgetOrPut` again. [Docs](https://nim-lang.org/docs/tab

Nested Tables

2021-10-30 Thread SunnyCorleone
That might be a way to do it but given I dont have a clue whats going on, im not going to use the code. Im just learning to use Nim last few days and nowhere near templates yet.

Nested Tables

2021-10-30 Thread Fire
More keys can be added this way. nested[k1] = {k2: {k3: @["Val0"]}.toTable()}.toTable() nested[k1][newkey] = {newkey1: @["Val1", "Val2", "Val3"]}.toTable() Run

Nested Tables

2021-10-30 Thread Hlaaftana
A way to do this would be: import std/tables var nested: Table[string, Table[string, Table[string, seq[string nested.mgetOrDefault(key1, initTable[string, Table[string, seq[string]]]()).mgetOrDefault(key2, initTable[string, seq[string]]())[key3] = @[1, 2, 3]

Nested Tables

2021-10-30 Thread SunnyCorleone
"Though honestly in most cases you're probably better off with a table like this: Table[(string, string, string), seq[string]]" This is possible but its not ideal for iteration or lookup in the resulting object though. Id have to to write a bunch more functions just to iterate the keys at each

Nested Tables

2021-10-30 Thread SunnyCorleone
This works but only for one set of keys.

Nested Tables

2021-10-30 Thread doofenstein
the problem you're having probably that you're trying to do exactly this in Nim: nested[key1][key2][key3] = [1, 2, 3] Run right? The thing is that if you're writing this: table[key] = value Run you're calling this function:

Nested Tables

2021-10-30 Thread Fire
This works for me, Nim 1.6.0: import std/tables var nested: Table[string, Table[string, Table[string, seq[string nested[k1] = {k2: {k3: @["Val"]}.toTable()}.toTable() nested[k1][k2][k3].add("NextVal") Run

Nested Tables

2021-10-30 Thread SunnyCorleone
These are something I use quite a lot in python like so: from collections import defaultdict nested = defaultdict(lambda: defaultdict(lambda: defaultdict(list))) nested[key1][key2][key3] = [1, 2, 3] Run Is something similar possible in Nim? I hav