Skip to content

Instantly share code, notes, and snippets.

@hrldcpr
hrldcpr / tree.md
Last active April 15, 2024 15:27
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@hrldcpr
hrldcpr / whyhaskelliscool.lhs
Created April 20, 2012 16:37
Why Haskell is Cool
Haskell is cool!
Here are some reasons why.
(This is a Literate Haskell file, so you can load it and then follow
along with the examples by running `ghci whyhaskelliscool.lhs`)
"Pattern matching" syntax for defining functions is cool, letting you
avoid 'if' statements and simply write out the different behaviors of
a function:
import collections
from typing import Counter, List, Set, Tuple
def subanagrams(letters: Counter[str], words: Set[str], min_len=4) -> List[Tuple[str, Counter[str]]]:
anagrams = []
for word in words:
if len(word) < min_len: continue
rest = letters.copy()
@hrldcpr
hrldcpr / concurrent_joystick.py
Last active March 9, 2019 23:15
read from a joystick device that blocks when held in the same direction, to control a character walking, using asyncio
import asyncio
import aiofiles # third party package, install from pip
# bytes outputted by the imaginary joystick device:
CENTER = b'x'
NORTH = b'n'
EAST = b'e'
SOUTH = b's'
@hrldcpr
hrldcpr / ramda.js
Last active August 31, 2017 19:53
exampda
// We have some state like:
const state1 = {
data: { some: 'stuff' },
people: ['blah', 'blah', 'blah'],
memberships: [
{ id: 1, roles: [], other: 'stuff' },
{ id: 2, roles: ['client'], more: 'junk' }
]
};
@hrldcpr
hrldcpr / ex-ramda.js
Last active August 31, 2017 19:51
ex-exampda
// We have some state like:
const state1 = {
data: { some: 'stuff' },
people: ['blah', 'blah', 'blah'],
memberships: [
{ id: 1, roles: [], other: 'stuff' },
{ id: 2, roles: ['client'], more: 'junk' }
]
};
@hrldcpr
hrldcpr / tee.py
Created July 12, 2015 22:08
implementation of `itertools.tee()` using one linked list instead of n deques
import collections
class LinkedList:
def __init__(self, value, tail=None):
self.value = value
self.tail = tail
def tee(iterable, n=2):
it = iter(iterable)
empty = LinkedList(None)

Keybase proof

I hereby claim:

  • I am hrldcpr on github.
  • I am harold (https://keybase.io/harold) on keybase.
  • I have a public key ASAPWKbiomRFjYfOwBXs914vgEEbl3rgCRofGekEWGPzUQo

To claim this, I am signing this object:

@hrldcpr
hrldcpr / iwish.hs
Last active December 12, 2015 12:39
dedup [] = []
dedup (x:xs) = dedup' x xs
where
dedup' x [] = [x]
dedup' x (x:xs) = dedup' x xs
dedup' y (x:xs) = y : dedup' x xs
for (var v = 0; v < 2*N; v++) {
for (var u = -N; u < N; u++) {
var w = getCube(u, v);
if (w > 0 && w < tick) { // there's a cube and it's not one we just created
setCube(u, v, 0); // remove it
if (u < N && v > 0) {
if (Math.random() < 0.5) // 50% of the time
setCube(u + 1, v - 1, tick); // directly above
if (Math.random() < 0.25) // 25% of the time
setCube(u + 1, v - 2, tick); // above-left