Skip to content

Instantly share code, notes, and snippets.

View badocelot's full-sized avatar

James M. Jensen II badocelot

View GitHub Profile
@badocelot
badocelot / lmc.py
Created March 28, 2018 04:03
Little-Man Computer Simulator + 4-bit Rule110 program for it
class LMComputer:
class Memory:
def __init__(self):
self.__cells = [0] * 100
def __getitem__(self, key):
return self.__cells[key]
def __setitem__(self, key, value):
if isinstance(key, int) and 0 <= key <= 99:
@badocelot
badocelot / weak.py
Last active March 22, 2018 02:57
Class for weakly-typed objects that obfuscate the wrapped object's type
import operator
__all__ = ['weak']
def apply_operator(op, a, b):
types = lambda x: x, float, int, complex
for a_type in types:
for b_type in types:
try:
return op(a_type(a), b_type(b))
@badocelot
badocelot / streams.py
Created March 21, 2018 07:58
Streams with a Python twist
import functools, itertools
class StreamIterator:
"""
Iterator class for a Stream
This class manages the cache for a regular Stream; it is unnecessary for a
ContainerStream.
"""
@badocelot
badocelot / linqlike.cpp
Created February 6, 2018 03:46
Linq-like classes in C++ (exercise)
#include <functional>
#include <iostream>
#include <string>
#include <vector>
template <typename InputIt> class From;
template <typename InputIt, typename OutType, typename InType> class Select;
template <typename InputIt, typename ItemType> class Where;
template <typename InputIt, typename OutType, typename InType>
@badocelot
badocelot / argcheck.py
Created October 10, 2017 16:08
Runtime type-checking for Python (unfinished from 2014)
import inspect
def name_of_type (t):
if "__class__" in dir(t):
return t.__class__.__name__
else:
return type(args[i]).__name__
def argtypes(*__argtypes_types__, **__argtypes_kwtypes__):
@badocelot
badocelot / db.py
Created September 1, 2015 20:12
Toward an rdbms in Python...
class Heading (set):
def __init__(self, *attributes):
if len(attributes) == 1 \
and isinstance(attributes[0], (list, tuple, set)):
attributes = attributes[0]
for att in attributes:
if not isinstance(att, str):
print(att)
raise TypeError("Attribute name must be string.")
if att == "":
@badocelot
badocelot / fp.py
Last active December 17, 2017 16:36
Support for some aspects of functional programming (argument piping, composition, and currying) for Python
import functools
from inspect import _empty, signature
class Function:
"""Decorator class for callables, making them curried and composable."""
class Recursor:
"""Wrapper class for identifying tail recursive calls to trampoline them."""
def __init__(self, fn):
@badocelot
badocelot / funcdec.py
Created June 27, 2013 04:10
Functional-paradigm Python decorators.
# Functional-paradigm decorators
def curried (function):
argc = function.__code__.co_argcount
# Subtract the defaults from the minimum arity
if function.__defaults__:
argc -= len(function.__defaults__)
# Pointless to curry a function that can take no arguments
@badocelot
badocelot / damlevdist.c
Created April 7, 2013 17:52
Damerau-Levenshtein edit distance implementation in C99. Based on pseudocode from Wikipedia: <https://en.wikipedia.org/wiki/Damerau-Levenshtein_distance> and on my Python implementation: <https://gist.github.com/badocelot/5327337>
// Damerau-Levenshtein edit distance implementation in C99
//
// Based on pseudocode from Wikipedia: <https://en.wikipedia.org/wiki/Damerau-Levenshtein_distance>
// and on my Python implementation: <https://gist.github.com/badocelot/5327337>
int damerau_levenshtein_distance (const char *source, const char *target)
{
// Size of the strings and dimensions of the matrix
int m = 0;
if (source != NULL)
m = strlen(source);
@badocelot
badocelot / damlevdist-improved.py
Last active May 22, 2020 16:35
Damerau-Levenshtein edit distance calculator in Python, with possible improvement. Based on pseudocode from Wikipedia: <https://en.wikipedia.org/wiki/Damerau-Levenshtein_distance>
# Damerau-Levenshtein edit distane implementation
# Based on pseudocode from Wikipedia: https://en.wikipedia.org/wiki/Damerau-Levenshtein_distance
# Copyright © 2013 James Jensen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is