Last active
August 29, 2015 14:07
-
-
Save jul/45485298a95cb74c224a to your computer and use it in GitHub Desktop.
having fun
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 ift(p, vt, vf): | |
return lambda val: (ift(*vt)(val) if isinstance(vt, tuple) else vt) \ | |
if p(val) else (ift(*vf)(val) if isinstance(vf, tuple) else vf) | |
compile = lambda f: sum([ int(bool(f(x)))<<x for x in range(0, 256) ] ) | |
interpret = lambda code: lambda val: int((1<<(val))&code != 0) | |
code_by3=compile( lambda x: (x%3)==0) | |
print bin(code_by3) | |
evaluate=interpret(code_by3) | |
sign = ift(lambda x:x>0, 1, (lambda y: y==0, 0, (lambda z:z<0, -1, 1))) | |
cmp = lambda x,y: sign(x-y) | |
print "\n".map(lambda x:"%r to 1 compares as %d " % (x,cmp(x,1)), range(10)) | |
print "\n".join(map(lambda x: "%d is %sdivsisble by 3" % (x, ift(evaluate, "", "not ")(x)), range(10))) |
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 ift(p, vt, vf): | |
return lambda val: (ift(*vt)(val) if isinstance(vt, tuple) else vt) \ | |
if p(val) else (ift(*vf)(val) if isinstance(vf, tuple) else vf) | |
compile = lambda f: sum([int(bool(f(x)))<<x for x in range(0, 256)]) | |
interpret = lambda code: lambda val: int((1<<(val))&code != 0) | |
code_by3 = compile(lambda x: (x%3)==0) | |
code_by2 = compile(lambda x: (x%2)==0) | |
code_by6 = code_by3&code_by2 | |
print bin(code_by3) | |
print bin(code_by6) | |
#unit_testing | |
assert code_by6 == compile(lambda x: (x%6)==0), "not same results" | |
assert str(bin(code_by6)).endswith("100001"), "not same code" | |
evaluate=interpret(code_by6) | |
print "\n".join(map(lambda x: "%d is %sdivsisble by 6" % (x, ift(evaluate, "", "not ")(x)), range(10))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment