Skip to content

Instantly share code, notes, and snippets.

@spitz-dan-l
spitz-dan-l / demo_parser_styles.ts
Last active June 28, 2019 14:50
Demo different parser styles
// This is what imperative style looks like when the parser is
// implemented with exceptions for control flow
function imperative_style_with_exceptions(p: Parser) {
p.consume('look at'); // could throw NoMatch
let who = p.split([ // could throw Split or NoMatch
() => p.consume('me', 'me'),
() => p.consume('mewtwo', 'mewtwo'),
() => p.consume('steven', 'steven'),
() => p.consume('martha', 'martha')
@spitz-dan-l
spitz-dan-l / update.ts
Last active April 26, 2019 19:49
update.ts - Concise, typed immutable updates to deeply-nested objects
/*
update.ts - Concise, typed immutable updates to deeply-nested objects
Daniel Spitz
Use it like this:
import {update} from 'update';
let obj = { a: 3, b: 'horse', c: { d: [1,2,3] }};
let obj2 = update(obj, { a: 0, c: { d: _ => [..._, 4] } });
@spitz-dan-l
spitz-dan-l / matching.py
Last active April 26, 2019 06:04
Python Atrocity: Structural Pattern Matching built on Exception Handling Syntax
import sys
import functools
"""
Python Atrocity: Structural Pattern Matching built on Exception Handling Syntax
Daniel Spitz
(Python 3.7+ only)
The goal of this Python Atrocity is to implement extensible, composable pattern
@spitz-dan-l
spitz-dan-l / graph_transform_pickle.py
Last active March 16, 2019 17:07
Python Atrocity: Graph-Transformation-Oriented Programming via Pickle
import pickle
import importlib
import pkg_resources
from operator import attrgetter
from functools import reduce
import io
from contextlib import ExitStack
from collections import defaultdict
"""
from distributed import Client, Queue, Variable, get_client, secede, rejoin, wait, fire_and_forget
from time import sleep
import gc
from functools import partial
from collections import Counter, defaultdict, namedtuple
from tornado import gen
import pandas as pd
@spitz-dan-l
spitz-dan-l / bad_monads.py
Last active February 28, 2019 17:55
Bad Monads
from functools import wraps
def do(lift_func=iter):
"""
Decorator that allows python generators to behave a bit like Haskell's do blocks.
The basic intuition to follow is that the Haskell "x <- y" syntax is mimicked in python by "x = yield y",
if it occurs inside a generator function decorated by @do().
By passing different arguments to @do(), you can change which monad type is mimicked.
from chatto_transform.schema.schema_base import cat
from libc.string cimport memcpy
from cpython cimport array
import array
import numpy as np
cimport numpy as np
import pandas as pd
from libc.stdlib cimport malloc, free
@spitz-dan-l
spitz-dan-l / multiple_dispatch.py
Last active June 8, 2017 14:34
Advanced python multiple dispatch implementation very advanced
import random #definitely a good thing to see at the top of a multiple-dispatch implementation!
import abc
import itertools
class ArgValidator:
@classmethod
def validate(cls, *args, mro_distances=None):
if mro_distances is None:
mro_distances = []
#disqualify validation rules with the wrong number of arguments
<html>
<style>
//put all styles here
body {
background: brown;
}
</style>
<body>
<!-- Put all html here -->
horse
public boolean compare(Grid<T> grid2) {
if(this.row != grid2.row || this.col != grid2.col) {
return false;
}
for(Grid<T>.Element tmpElem : this.elements()) {
Grid<T>.Element tmpElem2 = grid2.getElement(tmpElem.pos());
T v1 = tmpElem.value();
T v2 = tmpElem2.value();