Skip to content

Commit e44fb66

Browse files
author
czheo
committed
implement /to/ as generator
1 parent c1d08f0 commit e44fb66

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

syntax_sugar/infix.py

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from functools import partial, reduce
2+
from itertools import product, islice
23
from .util import flip
4+
from .composable import compose
35

46
class infix(partial):
57
def __truediv__(self, right):
@@ -12,26 +14,64 @@ def __rtruediv__(self, left):
1214
contains = infix(lambda lst, item: item in lst)
1315
pair = infix(lambda a, b: (a, b))
1416
join = infix(lambda lst, s: s.join(lst))
15-
x = infix(lambda l1, l2: list(map(lambda x, y: x * y, l1, l2)))
1617
hasattr = infix(hasattr)
1718
fmap = infix(flip(map))
1819
ffilter = infix(flip(filter))
1920
freduce = infix(flip(reduce))
21+
take = infix(compose(list, islice))
22+
23+
INF = float('inf')
24+
25+
class To:
26+
def __init__(self, start, end):
27+
if start /of/ int and (end /of/ int or end == INF):
28+
self.type = 'number'
29+
self.start = start
30+
self.curr = self.start
31+
self.end = end
32+
elif start /of/ str and end /of/ str:
33+
self.type = 'char'
34+
self.start = start
35+
self.curr = self.start
36+
self.end = end
37+
else:
38+
raise TypeError('Unknown range: %s to %s' % (start, end))
39+
40+
def __mul__(self, rhs):
41+
return product(self, rhs)
42+
43+
def __iter__(self):
44+
return self
45+
46+
def __next__(self):
47+
if self.type == 'number':
48+
if self.curr <= self.end:
49+
ret = self.curr
50+
self.curr += 1
51+
return ret
52+
else:
53+
raise StopIteration
54+
elif self.type == 'char':
55+
if ord(self.curr) <= ord(self.end):
56+
ret = self.curr
57+
self.curr = chr(ord(self.curr) + 1)
58+
return ret
59+
else:
60+
raise StopIteration
61+
else:
62+
raise StopIteration
63+
64+
def __str__(self):
65+
if self.type == 'number':
66+
return super(To, self).__str__()
67+
elif self.type == 'char':
68+
return ''.join(self)
69+
else:
70+
raise NotImplementedError
2071

2172
@infix
2273
def to(start, end):
23-
if start /of/ int and end /of/ int:
24-
return range(start, end + 1)
25-
elif start /of/ str and end /of/ str:
26-
ret = ''
27-
i = ord(start)
28-
while i <= ord(end):
29-
ret += chr(i)
30-
i += 1
31-
return ret
32-
else:
33-
raise TypeError('Unknown range: %s to %s' % (start, end))
34-
74+
return To(start, end)
3575

3676
__all__ = [
3777
'infix',
@@ -40,9 +80,10 @@ def to(start, end):
4080
'pair',
4181
'join',
4282
'to',
43-
'x',
83+
'INF',
4484
'hasattr',
4585
'fmap',
4686
'freduce',
4787
'ffilter',
88+
'take',
4889
]

tests/test_infix.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from syntax_sugar import *
22

33
def test_int_to_int():
4-
assert 1 /to/ 10 == range(1, 11)
4+
assert list(1 /to/ 10) == list(range(1, 11))
55

66
def test_str_to_str():
7-
assert 'A' /to/ 'Z' == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
7+
assert str('A' /to/ 'Z') == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
8+
def test_take():
9+
assert 1 /to/ INF /take/ 5 == [1,2,3,4,5]

0 commit comments

Comments
 (0)