Skip to content

Instantly share code, notes, and snippets.

View egregius313's full-sized avatar

Edward Minnix III egregius313

View GitHub Profile
@egregius313
egregius313 / timethis.py
Last active February 27, 2017 15:29
Simple context manager for timing Python code
#!/usr/bin/env python
from contextlib import contextmanager
import time
@contextmanager
def timethis(timer=time.perf_counter):
"""
Small timing function. It is meant to be used as a
context manager, and it returns a lambda that when
@egregius313
egregius313 / init_repr.py
Created March 5, 2017 05:15
A simple class decorator to define the __repr__ method based on the signature of the __init__ method.
"""
A simple class decorator to define the __repr__ method
based on the signature of the __init__ method.
"""
__author__ = 'egregius313'
_repr_template = """
def __repr__(self):
'Return repr(self)'
@egregius313
egregius313 / cross-product.clj
Last active March 9, 2017 15:46
Basic implementation of cross product using reduce.
(defn product [a & bs]
"The cross product of sets."
(reduce
(fn [xss ys]
(mapcat (fn [xs] (map (partial conj xs) ys)) xss))
(map vector a)
bs))
@egregius313
egregius313 / blob.clj
Created March 23, 2017 19:06
Basic Clojure implementation of the Blob problem for solving mazes
(ns blob)
(defn abnormal? [color]
(= color :abnormal))
(defn color [grid x y]
(get-in grid [x y]))
@egregius313
egregius313 / stop_writing_classes_game_of_life.py
Created August 25, 2017 01:15
Adaptation of the "Conway's Really Simple Game" code from Jack Diederich's talk "Stop Writing Classes".
# stop_writing_classes_game_of_life.py
"""
Adaptation of the "Conway's Really Simple Game" code
from Jack Diederich's talk "Stop Writing Classes".
Meant to help make some of the logic more explicit.
"""
from itertools import chain
@egregius313
egregius313 / maybe.py
Last active August 25, 2017 23:33
Basic implementation of the "Maybe a" types from Haskell in Python
# maybe.py
"""
Basic implementation of Haskell's Data.Maybe module.
The changes in the API are as follows:
- `maybe` is replaced with the `Maybe` class constructor
- `isJust` and `isNothing` are replaced with the builtin `isinstance`
- `listToMaybe` is replaced with the class method `Maybe.from_iterable`
- `maybeToList` is replaced with an implementation of `__iter__`,
so calls to `list()` will work
@egregius313
egregius313 / unpackable.py
Created December 23, 2017 18:31
Allow tuple unpacking on classes defined with @attr.s
# Allow tuple unpacking on classes defined with
# @attr.s decorator. Uses the attribute information
# from the definition of the class using attrs
#
# See also:
# http://www.attrs.org
# https://github.com/python-attrs/attrs
import hashlib
import linecache
@egregius313
egregius313 / ocaml_bootstrap.sh
Created January 17, 2018 17:09
Bootstrap an OCaml Installation on a Debian or OSX system
PKG_MGR=$(which apt-get brew apt | head -n1)
sudo $PKG_MGR update
sudo $PKG_MGR install -y opam
opam update
opam install core
if [[ $PKG_MGR =~ apt ]]
then
@egregius313
egregius313 / mac_bootstrap_gcc.sh
Created February 8, 2018 19:38
Script for bootstrapping gcc and g++ (4.9) on Mac OSX
#!/bin/bash
which brew || /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install gcc@4.9
echo "alias gcc='gcc-4.9'" >> .bash_profile
echo "alias g++='g++-4.9'" >> .bash_profile
@egregius313
egregius313 / check_cflags.py
Created February 16, 2018 17:29
check_cflags - guarantees that -Wall -Werror --pedantic flags are set in $CFLAGS
#!/usr/bin/env python
# check_cflags.py
"""
check_cflags - guarantees that -Wall -Werror --pedantic flags are set in $CFLAGS
Exits with 0 if proper CFLAGS is used, otherwise returns with 1
Usage:
./check_cflags.py FILE
"""