Skip to content

Instantly share code, notes, and snippets.

#![feature(box_patterns)]
#[derive(Debug)]
#[derive(Clone)]
pub enum Syntax {
Param(usize),
Constant(i32),
SymbolicValue,
Add(Box<Syntax>, Box<Syntax>),
Mul(Box<Syntax>, Box<Syntax>),
@sjkillen
sjkillen / DeepComparator.py
Created November 17, 2019 23:21
Lazy object hashing/equality checking for acyclic objects
def refineddir(obj):
return tuple(prop for prop in dir(obj) if not prop.startswith("_") and not hasattr(getattr(obj, prop), "__call__"))
class DeepComparator:
def __init__(self, wrap):
self.wrap = wrap
def __eq__(self, other):
if isinstance(self, DeepComparator):
self = self.wrap
if isinstance(other, DeepComparator):
@sjkillen
sjkillen / overloading.c
Created June 9, 2019 03:52
Function overloading in C
#include <stdio.h>
#include <stdlib.h>
enum discriminateFunc {
NO_RETURN,
INT_RETURN,
INT_RETURN_TAKE_INT,
};
union funcUnion {
@sjkillen
sjkillen / adding_types.ts
Created May 10, 2019 23:52
Perform addition arithmetic and equality testing entirely in TypeScript's type system
// i++
interface Inc<T> {
1: Inc<this>
t: T
}
// i--
interface Dec<T> {
0: T extends Inc<infer T> ? T : never
}
/**
* Manage a table of IP addresses
* Supports the following operations:
* adding an IP
* removing an IP
* checking if an IP is in the table
* iterating all IP addresses inside the table
*/
/**
let rejectA;
const promA = new Promise((_, _reject) => rejectA = _reject);
let rejectB;
const promB = new Promise((_, _reject) => rejectB = _reject);
async function* foo() {
try {
await promA.finally(() => console.log("Promise A no longer pending"));
await promB.finally(() => console.log("Promise B no longer pending"));
} catch (e) {
const pure = Symbol("pure function");
const scanDone = Symbol();
function scanParam(func) {
if (func.length !== 1) {
throw "Only works with unary functions";
}
const shape = {};
try {
@sjkillen
sjkillen / fpprim.js
Created May 22, 2018 15:22
FP primitive functions for use with iterators and the bind operator
/**
* Bindable method for flattening iterables
* @this {Iterable<T>}
* @param {(item: T) => J | void} mapper
* @returns flattened version of iterable with elements mapped
* if a mapper function was given
*/
export function *flattenAll(mapper = v => v) {
for (const item of this) {
/*
* Spencer Killen
* CMPT 360
* Assignment 3
* Dec 5th 2016
* Library used for applying a file lock after a condition is met
*/
#include <sys/inotify.h>
#include <aio.h>
interface EventRegister<T> {
event: keyof T,
// any used here because ts is picky, doesn't make a difference
// may be able to replace with T[keyof T] in future versions
callback: {(data: any): void},
once: boolean
}
/**
* Basic callback based publish / subscribe baseclass