Skip to content

Instantly share code, notes, and snippets.

View greezybacon's full-sized avatar

Jared Hancock greezybacon

View GitHub Profile
@greezybacon
greezybacon / newton.py
Created December 10, 2012 16:33
Newton's method for root of 33
def newton_method(x0, fn, dfn):
xn = x0
while True:
xn -= fn(xn) / dfn(xn)
yield xn
root_33 = newton_method(1, lambda x: 33 - x*x, lambda x: -2*x)
for x in range(6):
print(next(root_33))
@greezybacon
greezybacon / memoize.py
Created August 3, 2012 13:30
Memoized decorators for python
def memoized(func):
"""
@see http://en.wikipedia.org/wiki/Memoization
@see http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
Decorator used for deterministic functions: functions which will always
return the same result for the same arguments. Functions which are
expensive (perhaps involving database queries, read and parse files,
etc.) can be memoized: cached into memory, so that the result can
simply be retrieved if the function is called again with the same
@greezybacon
greezybacon / overloaded.py
Created June 21, 2012 20:51
Python overloaded method decorator
class overloaded(object):
"""
Simple (well, simple is relative) tool to allow for overloaded methods
in Python. Overloaded methods are methods with the same name that
receive different types of arguments. Statically typed languages such as
C++ support overloaded methods at compile time; however, dynamically
typed languages such as Python cannot support such methods, because no
type hints can be specified in the function definition.
This class operates as a decorator and allows for a cascaded design of