This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |