Skip to content

Instantly share code, notes, and snippets.

View MichaelSnowden's full-sized avatar

Michael Snowden MichaelSnowden

View GitHub Profile
@MichaelSnowden
MichaelSnowden / main.go
Created May 17, 2023 20:09
How to emulate virtual methods in Go
// Package main demonstrates how to emulate virtual methods in Go.
// This implementation doesn't show what the constructors should be; nor does it practice proper information-hiding.
package main
import (
"fmt"
"math"
)
type Shape interface {
@MichaelSnowden
MichaelSnowden / propagate_cancel.go
Created April 25, 2023 17:57
A method of propagating cancellation signals to a context
package signalprop
import (
"context"
)
func PropagateCancel(ctx context.Context, done chan struct{}) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(ctx)
go func() {
select {
@MichaelSnowden
MichaelSnowden / NonEmpty.elm
Last active August 23, 2020 17:20
Reservoir Sampling in Elm
module NonEmpty exposing (..)
type alias NonEmpty a =
{ head : a, tail : List a }
@MichaelSnowden
MichaelSnowden / map_dag.c
Created August 14, 2020 03:36
A pattern for applying a map function to a dag in C without using malloc.
#include <stdio.h>
#include <zconf.h>
#include <stdlib.h>
#include <assert.h>
typedef struct IntNode {
int leaf;
int value;
int numChildren;
struct IntNode **children;
@MichaelSnowden
MichaelSnowden / test_runner.c
Created August 9, 2020 06:30
C Test Runner
#include <stdlib.h>
#include <zconf.h>
#include <stdio.h>
#include <fcntl.h>
#include <memory.h>
#include <errno.h>
#include <assert.h>
#include "test_runner.h"
int runTests(Test *tests, int numTests) {
@MichaelSnowden
MichaelSnowden / json.c
Created July 29, 2020 04:09
Comparison of jsonArray printing methods. I have no idea why, but for some reason the generic approach is virtually the same speed as the specific approach. Almost as if the function call overhead is negligible.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int Foo;
// repetitive approach
int foosToJsonArray(FILE *file, Foo *foos, int n) {
if (fputc('[', file) == EOF) {
return -1;
@MichaelSnowden
MichaelSnowden / cps.c
Last active July 22, 2020 06:27
An extremely bad idea for a way to implement continuation passing style via setjmp and longjmp and a not-so-bad idea
#include <string.h>
#include <stdio.h>
#include <setjmp.h>
#include <unistd.h>
typedef struct {
char *data;
size_t offset;
size_t capacity;
} Buffer;
@MichaelSnowden
MichaelSnowden / neural_autocorrect.ipynb
Last active September 4, 2018 02:21
An autocorrect tool that functions by mapping words to vectors, and levenshtein distance to euclidean distance
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@MichaelSnowden
MichaelSnowden / taskmanager.hs
Last active December 9, 2017 21:29
Haskell Todo list CLI
import Data.List
import Prelude hiding (id, pure)
import System.Directory
import System.Exit
import System.IO
import System.IO.Error
import System.Process
import Text.Read
data Route
import cv2
import numpy as np
import sys
if len(sys.argv) < 3:
print 'Usage: python match.py <template.png> <image.png>'
sys.exit()
template_path = sys.argv[1]
template = cv2.imread(template_path, cv2.IMREAD_UNCHANGED)