Created
September 12, 2014 14:37
-
-
Save halgari/6472dd2f801fb8867ab6 to your computer and use it in GitHub Desktop.
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
import time | |
class Reduced(object): | |
def __init__(self, itm): | |
self._itm = itm | |
def deref(self): | |
return self._itm | |
def reduced(x): | |
return Reduced(x) | |
def is_reduced(x): | |
return isinstance(x, Reduced) | |
def transduce(xform, rfn, lst): | |
f = xform(rfn) | |
acc = f() | |
for item in lst: | |
acc = f(acc, item) | |
if is_reduced(acc): | |
return f(acc) | |
return f(acc) | |
data = list(range(1024)) | |
dataf = list(map(float, range(1024))) | |
def map(f): | |
def xform_fn(xf): | |
def map_rfn(*args): | |
l = len(args) | |
if l == 0: | |
return xf() | |
if l == 1: | |
return xf(args[0]) | |
if l == 2: | |
return xf(args[0], f(args[1])) | |
return map_rfn | |
return xform_fn | |
def filter(f): | |
def xform_fn(xf): | |
def map_rfn(*args): | |
l = len(args) | |
if l == 0: | |
return xf() | |
if l == 1: | |
return xf(args[0]) | |
if l == 2: | |
if f(args[1]): | |
return xf(args[0], args[1]) | |
return map_rfn | |
return xform_fn | |
def inc(x): | |
return x + 1 | |
def add(*args): | |
l = len(args) | |
if l == 0: | |
return 0 | |
if l == 1: | |
return args[0] | |
if l == 2: | |
return args[0] + args[1] | |
for x in range(1000): | |
t = time.time() | |
#for x in range(1024): | |
# transduce(map(inc), add, data) | |
#print (time.time() - t) * 1000 | |
t = time.time() | |
for x in range(1024): | |
transduce(map(inc), add, dataf) | |
print (time.time() - t) * 1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment