narimiran / advent_of_nim_2016

Learning Nim by solving AoC 2016 tasks
2 stars 0 forks source link

In-place dict in Nim #1

Closed cdunn2001 closed 6 years ago

cdunn2001 commented 6 years ago

https://github.com/narimiran/advent_of_nim_2016/blob/b0fd9cde917083756c918b850551ccd7d6ee893c/day01.nim#L6

In your Python code, I see this:

ROTATION = {
    'L': 1j,
    'R': -1j
}

You can do that in Nim too, basically:

import tables, complex
let ROTATION = {
    'L': (re:0.0, im:  1.0),
    'R': (re:0.0, im: -1.0)
}.toTable()
echo $ROTATION
{R: (0.0, -1.0), L: (0.0, 1.0)}

The trick is toTable().

var direction: Complex = (0.0, 1.0)
direction *= ROTATION['R']
echo direction
from typetraits import nil
echo typetraits.name(type(ROTATION))
(1.0, 0.0)
Table[system.char, tuple[re: float64, im: float64]]

Note that you need decimal points for floats, and a space after the :.

narimiran commented 6 years ago

Thank you for taking the time and the detailed (easy to follow) suggestion. I just pushed the changes as per your suggestion.

Fortunately, I'm conditioned (PEP8) to use space after :, but even still - hopefully this bug will be sorted out in some future version of Nim.

If you find anything else worth changing/improving, let me know.