Last active
August 29, 2015 14:19
-
-
Save sirpercival/9c707c39e48559d496b2 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
>>> m('?@$..!!+.!!+>!.!!!!+?/!!!@!@>!!!') | |
pushing 0 | |
[0] | |
pushing say_1 | |
[0, (1, 'say_1')] | |
pushing call_2 | |
[0, (1, 'say_1'), (2, 'call_2')] | |
pushing chain_2 | |
[0, (1, 'say_1'), (2, 'call_2'), (2, 'chain_2')] | |
pushing chain_2 | |
[0, (1, 'say_1'), (2, 'call_2'), (2, 'chain_2'), (2, 'chain_2')] | |
function chain_2 acting on chain_2 | |
[0, (1, 'say_1'), (2, 'call_2'), (1, 'partial_chain_2_chain_2')] | |
function partial_chain_2_chain_2 acting on call_2 | |
pushing composition_n for chain_2 and call_2 | |
[0, (1, 'say_1'), (2, 'composition_n')] | |
pushing clone_1 | |
[0, (1, 'say_1'), (2, 'composition_n'), (1, 'clone_1')] | |
pushing chain_2 | |
[0, (1, 'say_1'), (2, 'composition_n'), (1, 'clone_1'), (2, 'chain_2')] | |
function chain_2 acting on clone_1 | |
[0, (1, 'say_1'), (2, 'composition_n'), (1, 'partial_chain_2_clone_1')] | |
function partial_chain_2_clone_1 acting on composition_n | |
pushing composition_n for clone_1 and composition_n | |
[0, (1, 'say_1'), (1, 'composition_n')] | |
pushing clone_1 | |
[0, (1, 'say_1'), (1, 'composition_n'), (1, 'clone_1')] | |
pushing shift_1 | |
[0, (1, 'say_1'), (1, 'composition_n'), (1, 'shift_1')] | |
function shift_1 acting on composition_n | |
shifting composition_n | |
[0, (1, 'say_1'), (2, 'shifted_n')] | |
pushing chain_2 | |
[0, (1, 'say_1'), (2, 'shifted_n'), (2, 'chain_2')] | |
function chain_2 acting on shifted_n | |
[0, (1, 'say_1'), (1, 'partial_chain_2_shifted_n')] | |
function partial_chain_2_shifted_n acting on say_1 | |
pushing composition_n for shifted_n and say_1 | |
[0, (2, 'composition_n')] | |
function composition_n acting on 0 | |
[(1, 'partial_composition_n_0')] | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
File "shift.py", line 86, in m | |
i(e[_]) | |
File "shift.py", line 13, in i | |
def i(x):x(*[o() for _ in j(x)]) #pop args, call func | |
IndexError: pop from empty list |
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 pdb | |
def m_ungolfed(t): | |
from functools import partial as b #this is our currier | |
k,d=[],[] #stack, output string | |
u,o=k.append,k.pop #push & pop | |
w=lambda x:x is 0 #quick checker for func vs blank | |
j=lambda x:range(x.a) #iterate over arity | |
nm = lambda x:x.__name__ if not w(x) else str(x) #debugging | |
def a(n): #decorator to assign an arity to a function | |
def f(x):x.a=n;return x | |
return f | |
def i(x):x(*[o() for _ in j(x)]) #pop args, call func | |
@a(2) | |
def apply_2(x,y): | |
print 'function '+nm(x)+' acting on '+nm(y) | |
#x(y) if x.a==1 else u(a(x.a-1)(b(x,y))) | |
if x.a==1: | |
x(y) | |
else: | |
_ = a(x.a-1)(b(x,y)) | |
_.__name__ = 'partial_'+nm(x)+'_'+nm(y) | |
u(_) #p | |
@a(0) | |
def blank_0(): | |
print 'pushing 0' | |
u(0) #q | |
@a(0) | |
def c(): | |
print 'pushing clone_1' | |
@u | |
@a(1) | |
def clone_1(x): | |
print 'cloning '+nm(x) | |
u(x);u(x) #f | |
@a(0) | |
def v(): | |
print 'pushing shift_1' | |
@u | |
@a(1) | |
def shift_1(x): #y | |
print 'shifting '+nm(x) | |
@u | |
@a(x.a+1) | |
def shifted_n():_=o();i(x);u(_) #f | |
@a(0) | |
def r(): | |
print 'pushing fork_3' | |
@u | |
@a(3) | |
def fork_3(x,y,z): | |
print 'x is '+nm(x)+', so push '+[nm(y),nm(z)][w(x)] | |
u(y if w(x) else z) #f | |
@a(0) | |
def l(): #u(p) | |
print 'pushing call_2' | |
@u | |
@a(2) | |
def call_2(x,y): | |
print 'function '+nm(x)+' acting on '+nm(y) | |
x(y)if x.a==1 else u(a(x.a-1)(b(x,y))) | |
@a(0) | |
def n(): | |
print 'pushing chain_2' | |
@u | |
@a(2) | |
def chain_2(x,y):#f | |
print 'pushing composition_n for '+nm(x)+' and '+nm(y) | |
@u | |
@a(x.a) | |
def composition_n(*_): | |
pdb.set_trace() | |
print type(nm(y)), type(y) | |
print 'calling '+nm(x)+' then '+nm(y) | |
x(*_);i(y)#h | |
@a(0) | |
def s(): | |
print 'pushing say_1' | |
@u | |
@a(1) | |
def say_1(x): | |
print 'x is '+nm(x)+', so saying '+['1','0'][w(x)] | |
d.append(['1','0'][w(x)]);u(x)#f | |
#e=dict(zip('!?+>/$.@',[p,q,c,v,r,l,n,s])) | |
e=dict(zip('!?+>/$.@',[apply_2,blank_0,c,v,r,l,n,s])) | |
for _ in t: | |
try: | |
i(e[_]) | |
except RuntimeError: | |
break | |
print [(_.a,nm(_)) if not w(_) else _ for _ in k] | |
print ''.join(d) |
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
>>> m('?@!@@!') | |
pushing 0 | |
[0] | |
pushing say_1 | |
[0, (1, 'say_1')] | |
function say_1 acting on 0 | |
x is 0, so saying 0 | |
[0] | |
pushing say_1 | |
[0, (1, 'say_1')] | |
pushing say_1 | |
[0, (1, 'say_1'), (1, 'say_1')] | |
function say_1 acting on say_1 | |
x is say_1, so saying 1 | |
[0, (1, 'say_1')] | |
01 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment