Skip to content

Commit 5cc5bba

Browse files
author
czheo
committed
add match
1 parent 0609aee commit 5cc5bba

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

syntax_sugar/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
from .infix import *
55
from .stream import *
66
from .placeholder import *
7+
from .match import *
78
from eventlet import green

syntax_sugar/match.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from .pipe import END
2+
3+
__all__ = [
4+
'match',
5+
]
6+
7+
class match:
8+
def __init__(self, x):
9+
self.x = x
10+
self.done = False
11+
self.result = None
12+
13+
def __or__(self, rhs):
14+
if rhs is END:
15+
return self.result
16+
if self.done:
17+
return self
18+
if isinstance(rhs, dict):
19+
result = rhs.get(self.x, None)
20+
if result:
21+
self.done = True
22+
self.result = result
23+
return self
24+
if isinstance(rhs, tuple):
25+
patt, fn = rhs
26+
if self.x == patt:
27+
self.done = True
28+
self.result = fn()
29+
return self
30+
else:
31+
raise SyntaxError('Bad match syntax.')

0 commit comments

Comments
 (0)