Created
June 7, 2018 11:09
-
-
Save danilomo/93622c6051ce54276c98c062bb10ca78 to your computer and use it in GitHub Desktop.
Emulating bash's chain of commands ( && operator )
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 LinkedList: | |
def __init__(self, head, tail): | |
self.head = head | |
self.tail = tail | |
def reverse(self): | |
l = self | |
newl = None | |
while l: | |
newl = cons(l.head, newl) | |
l = l.tail | |
return newl | |
def __and__(self, f): | |
return cons( f, self ) | |
def foreach(self): | |
l = self | |
while l: | |
print(l.head) | |
l = l.tail | |
def __call__(self): | |
it = self.reverse() | |
while it: | |
try: | |
it.head() | |
except: | |
return False | |
it = it.tail | |
return True | |
cons = LinkedList | |
class Proc: | |
def __init__(self,f): | |
self.f = f | |
def __call__(self, *args, **kvargs) : | |
return self.f(*args, **kvargs) | |
def __and__(self, f2): | |
return cons( f2, cons(self, None) ) | |
def __str__(self): | |
return self.f.__name__ | |
def proc( f ): | |
return Proc(f) | |
@proc | |
def f1(): | |
print("F1") | |
@proc | |
def f2(): | |
print("F2") | |
@proc | |
def f3(): | |
print("F3") | |
@proc | |
def failure(): | |
raise Exception('My error!') | |
#l = cons( "a", cons( "b", cons( "c", None ) ) ) | |
l = f1 & f2 & f3 | |
if l(): | |
print("The chain was executed successfully") | |
l = f1 & f2 & failure & f3 | |
print("------") | |
if l(): | |
print("The chain was executed successfully") | |
else: | |
print("Something abnormal ocurred") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment