Re: Macro to create a dictionary (table) like in python!
I did this small test and I don't see any problems or incompatibility. Can someone show me where the use of the "%" can fail? Can someone show me an example where the "%" is used in JSON as in tables "%{}"? import tables from strutils import `%` import json proc `%`[T,G,H](a : array[T,(G,H)]) : Table[G,H] = a.toTable() # Table var h = %{"key_1": "value_1", "key_2": "value_2"} echo h echo h["key_1"] # JSON var j = %*{"key_1": "value_1", "key_2": "value_2"} echo j echo j["key_1"] Run Result: $ nim c -r test.nim Hint: operation successful (34378 lines compiled; 8.349 sec total; 46.828MiB peakmem; Debug Build) [SuccessX] Hint: /home/hdias/Downloads/test [Exec] {"key_1": "value_1", "key_2": "value_2"} value_1 {"key_1": "value_1", "key_2": "value_2"} "value_1" Run
Re: Macro to create a dictionary (table) like in python!
Github pull request: [https://github.com/nim-lang/Nim/pull/11702](https://github.com/nim-lang/Nim/pull/11702).
Re: Macro to create a dictionary (table) like in python!
These association lists, often abbreviated "alist", get a lot of play in lisp, but also OCaml, Haskell, etc.. [https://en.wikipedia.org/wiki/Association_list](https://en.wikipedia.org/wiki/Association_list)
Re: Macro to create a dictionary (table) like in python!
> What prevents it from being used only like this without "%", "@" or "toTable"? Just a two comments above: > `{}` is a sugar for array of tuples .
Re: Macro to create a dictionary (table) like in python!
{} is a sugar for array of tuples, so @ already works with it by just making a sequence. echo @{"key_1": "value_1", "key_2": "value_2"} Run results in @[("key_1", "value_1"), ("key_2", "value_2")] Run
Re: Macro to create a dictionary (table) like in python!
I would have to agree with `@`. +1 vote.
Re: Macro to create a dictionary (table) like in python!
I think using @ instead of the % is perfect. Its just like the array syntax. var a = @[1, 2, 3] var b = @{"key_1": "value_1", "key_2": "value_2"} Run
Re: Macro to create a dictionary (table) like in python!
is sugar, so a literal is also possible.
Re: Macro to create a dictionary (table) like in python!
The T should probably be renamed N for convention, possibly tagging it with static int as well. Also it's true that {key: value} syntax creating an array of tuples is not well documented/visible.
Re: Macro to create a dictionary (table) like in python!
Great. Works. Thank you.
Re: Macro to create a dictionary (table) like in python!
. import tables #constructs an array of tuples #{"key_1": "value_1", "key_2": "value_2"} proc `%`[T,G,H](a : array[T,(G,H)]) : Table[G,H] = a.toTable() var a = %{"key_1": "value_1", "key_2": "value_2"} Run
Macro to create a dictionary (table) like in python!
Is it possible to create a dictionary like python by typing only: var a = {"key_1": "value_1", "key_2": "value_2"} Run Instead of? var a = {"key_1": "value_1", "key_2": "value_2"}.toTable Run or something similar to what is used by sequences: var x = @[1, 2, 3] var a = %{"key_1": "value_1", "key_2": "value_2"} Run It reminds me Perl arrays (@) and hashes (%). How to make a macro to implement the "%" before "{}"?