Skip to content

Commit 7180728

Browse files
author
czheo
committed
support -inf in /to/
1 parent baa6410 commit 7180728

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

syntax_sugar/infix.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ def __rtruediv__(self, left):
2121
take = infix(compose(list, islice))
2222

2323
INF = float('inf')
24+
NEGINF = float('-inf')
2425

2526
class To:
2627
def __init__(self, start, end):
27-
if start == INF:
28-
raise TypeError('Cannot start range from infinity')
28+
if start in {INF, NEGINF}:
29+
raise ValueError('Cannot start range from infinity')
2930

3031
valid_char = lambda c: c /of/ str and len(c) == 1
31-
valid_integer = lambda i: i /of/ int or i == INF
32+
valid_integer = lambda i: i /of/ int or i == INF or i == NEGINF
3233

3334
if valid_integer(start) and valid_integer(end):
3435
self.type = 'number'
@@ -127,6 +128,7 @@ def by(to_object, step):
127128
'to',
128129
'by',
129130
'INF',
131+
'NEGINF',
130132
'hasattr',
131133
'fmap',
132134
'freduce',

tests/test_infix.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ def test_bad_step():
6969
with raises(ValueError):
7070
to_obj.step = 1
7171

72+
def test_infinity():
73+
with raises(ValueError):
74+
INF /to/ 100
75+
76+
with raises(ValueError):
77+
NEGINF /to/ 1
78+
79+
assert 1 /to/ INF /take/ 10 == list(range(1, 11))
80+
assert 1 /to/ NEGINF /take/ 10 == list(range(1, -9, -1))
81+
82+
assert 1 /to/ INF /by/ 2 /take/ 10 == list(range(1, 100, 2))[:10]
83+
assert 1 /to/ NEGINF /by/ 2 /take/ 10 == list(range(1, -100, -2))[:10]
84+
7285
def test_take():
7386
assert 1 /to/ INF /take/ 5 == [1,2,3,4,5]
7487

0 commit comments

Comments
 (0)