Skip to content

Instantly share code, notes, and snippets.

View p7g's full-sized avatar

Patrick Gingras p7g

View GitHub Profile

Keybase proof

I hereby claim:

  • I am ging0044 on github.
  • I am pat775 (https://keybase.io/pat775) on keybase.
  • I have a public key ASCCUNP1Cc0z7h_2iPpYE9a4aBcM77Ahzx8x7vjITGKCNAo

To claim this, I am signing this object:

Keybase proof

I hereby claim:

  • I am p7g on github.
  • I am pat775 (https://keybase.io/pat775) on keybase.
  • I have a public key ASAErLL4IaZn9nL5IvJl9aVtsZDhRzfHnFNvePtWmGSjkQo

To claim this, I am signing this object:

@p7g
p7g / cpp_fib.h
Last active June 2, 2019 16:50
Compile-time fibonacci generator with C preprocessor macros. Based on the work of @orangeduck, see orangeduck/CPP_COMPLETE
#define EVAL(...) __VA_ARGS__
#define $0 0
#define IF_ELSE(C, T, E) JOIN(IF_ELSE_, C)(T, E)
#define IF_ELSE_0(T, E) E
#define IF_ELSE_1(T, E) T
#define IF(C, T) IF_ELSE(C, T,)
#define KICK(C, $) IF_ELSE(C, INC($), KICKED)
@p7g
p7g / template_fib.cc
Created June 2, 2019 16:55
Fibonacci up to 30 with C++ templates
template <int N>
struct Fib {
enum {
value = Fib<N - 2>::value + Fib<N - 1>::value,
};
};
template <>
struct Fib<1> {
enum {
@p7g
p7g / remove_python_types.vim
Created August 5, 2019 17:34
Some vim :s commands to remove python type hints
nnoremap <leader>z :%s/ -> [^:]\+:/:/gc<CR>
nnoremap <leader>T :%s/\([^']\): [^,)]\+\([,)]\)/\1\2/gc<CR>
@p7g
p7g / hashmap.py
Created September 16, 2019 22:06
A hashmap implemented in python
FNV_PRIME_64 = 1099511628211
FNV_OFFSET_BASIS_64 = 14695981039346656037
def hash(s: str) -> int:
val = FNV_OFFSET_BASIS_64
for c in s:
val ^= ord(c)
val *= FNV_PRIME_64
return val
@p7g
p7g / roll.py
Last active September 29, 2019 00:09
Dice roller script
#!/usr/bin/env python3
from collections import defaultdict
from secrets import token_bytes
def roll(num_sides: int) -> int:
assert num_sides > 0, 'cannot have no sides'
return int.from_bytes(token_bytes(48), 'little') % num_sides + 1
@p7g
p7g / objects.py
Last active October 3, 2019 12:36
A little object system written in Python, inspired by https://www.aosabook.org/en/500L/a-simple-object-model.html
class Type:
def __init__(self, name, *, method_table=None, parent=None):
self.name = name
self.method_table = method_table or {}
self.parent = parent
def resolve_method(self, name, /):
if name in self.method_table:
return self.method_table[name]
elif self.parent is not None:
@p7g
p7g / obj.py
Last active October 24, 2019 03:17
A smalltalk/ruby-style object model implemented in python
from typing import Any, Dict, Optional
from dataclasses import dataclass
MISSING = 'missing'
def mro(o):
yield o
@p7g
p7g / bench.b
Created March 23, 2020 13:15
BF interpreter python/pypy vs rust (based on kostya/benchmarks)
Benchmark brainf*ck program
>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
[>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
++++++++[>++++++++++[>++++++++++[>++++++++++[>+
+++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++.