chappers : Nim Python Starting Out

29 Mar 2020

In an effort to learn something new and speed up some existing experiments, I’ve started dipping my toes into the Nim programming language.

In theory it should offer a substatial speed up to parts of my Python code but we’ll see what actually happens. Here are some patterns which I’m sure I’ll need to refer multiple times in the future.

Sorting a Table

To sort a table by value, you need to create something to help with the sorting.

import tables

var a = initOrderedTable[int, array[4, float]]()
for idx, el in [1,2,3]:
  var newval = if idx==1: 10.0 else: -1.0
  a[el] = [float(idx), float(-idx), float(newval), float(idx)]

echo a

echo "\tSort by idx 1"
a.sort(proc(x, y: (int, array[4, float])): int =
  result = cmp(x[1][1], y[1][1]))
echo a

echo "\tSort by idx 2"
a.sort(proc(x, y: (int, array[4, float])): int =
  result = cmp(x[1][2], y[1][2]))
echo a

Output:

{1: [0.0, 0.0, -1.0, 0.0], 2: [1.0, -1.0, 10.0, 1.0], 3: [2.0, -2.0, -1.0, 2.0]}
        Sort by idx 1
{3: [2.0, -2.0, -1.0, 2.0], 2: [1.0, -1.0, 10.0, 1.0], 1: [0.0, 0.0, -1.0, 0.0]}
        Sort by idx 2
{3: [2.0, -2.0, -1.0, 2.0], 1: [0.0, 0.0, -1.0, 0.0], 2: [1.0, -1.0, 10.0, 1.0]}



more...


2020

March

07 Auto Differentiation From Scratch

February

21 Notes On Extending Multiagent Environments

17 Plan Attend Generate In Pytorch

January

08 Beyond Gridworld

2019

December

26 My Notes On Graph Neural Networks

15 Reproducibility Is Overrated

November

08 Using Spektral Top K Api

October

12 How Hard Could It Be

September

05 Thinking About Linear Ensembles

August

13 Building Out Stable Baseline Benchmarks

:: All Posts