Skip to content

Commit 4d86105

Browse files
author
czheo
committed
make compose infix
1 parent 6da362a commit 4d86105

File tree

6 files changed

+54
-50
lines changed

6 files changed

+54
-50
lines changed

syntax_sugar/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55
from .stream import *
66
from .placeholder import *
77
from .match import *
8-
from eventlet import green

syntax_sugar/composable.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from functools import partial
22
from functools import reduce
3+
from .infix import infix
34

45

56
__all__ = [
67
'composable',
78
'compose',
9+
'o',
810
]
911

1012
class composable(partial):
@@ -14,7 +16,10 @@ def __mul__(self, rhs):
1416
def __rmul__(self, lhs):
1517
return lambda *args, **kwargs: lhs(self(*args, **kwargs))
1618

19+
@infix
1720
def compose(*args):
1821
return reduce(lambda acc, fn:
1922
(lambda *ag, **kwag: acc(fn(*ag, **kwag))),
2023
args)
24+
25+
o = compose

syntax_sugar/infix.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,12 @@
11
from functools import partial
2-
from .iter import Iterator, Range
32

43
__all__ = [
54
'infix',
6-
'is_a',
7-
'as_a',
8-
'to',
9-
'step',
10-
'INF',
11-
'NEGINF',
12-
'has',
13-
'take',
14-
'drop',
155
]
166

17-
INF = float('inf')
18-
NEGINF = float('-inf')
19-
207
class infix(partial):
218
def __truediv__(self, right):
229
return self(right)
2310

2411
def __rtruediv__(self, left):
2512
return infix(self.func, left)
26-
27-
is_a = infix(isinstance)
28-
has = infix(hasattr)
29-
30-
@infix
31-
def as_a(obj, clazz):
32-
return clazz(obj)
33-
34-
@infix
35-
def to(start, end):
36-
return Iterator(Range(start, end))
37-
38-
@infix
39-
def step(obj, step):
40-
step = abs(step)
41-
obj = Iterator(obj) if not obj /is_a/ Iterator else obj
42-
return obj.slice(step=step)
43-
44-
@infix
45-
def take(obj, n):
46-
obj = Iterator(obj) if not obj /is_a/ Iterator else obj
47-
return obj.slice(stop=n)
48-
49-
@infix
50-
def drop(obj, n):
51-
obj = Iterator(obj) if not obj /is_a/ Iterator else obj
52-
return obj.slice(start=n)

syntax_sugar/util.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
1+
from .infix import infix
2+
from .iter import Iterator, Range
3+
4+
__all__ = [
5+
'flip',
6+
'is_a',
7+
'as_a',
8+
'to',
9+
'step',
10+
'INF',
11+
'has',
12+
'take',
13+
'drop',
14+
]
15+
16+
INF = float('inf')
17+
118
def flip(fn):
219
def wrapper(*args):
320
return fn(args[1], args[0])
421
return wrapper
522

6-
__all__ = [
7-
'flip',
8-
]
23+
is_a = infix(isinstance)
24+
has = infix(hasattr)
25+
26+
@infix
27+
def as_a(obj, clazz):
28+
return clazz(obj)
29+
30+
@infix
31+
def to(start, end):
32+
return Iterator(Range(start, end))
33+
34+
@infix
35+
def step(obj, step):
36+
step = abs(step)
37+
obj = Iterator(obj) if not obj /is_a/ Iterator else obj
38+
return obj.slice(step=step)
39+
40+
@infix
41+
def take(obj, n):
42+
obj = Iterator(obj) if not obj /is_a/ Iterator else obj
43+
return obj.slice(stop=n)
44+
45+
@infix
46+
def drop(obj, n):
47+
obj = Iterator(obj) if not obj /is_a/ Iterator else obj
48+
return obj.slice(start=n)

tests/test_infix.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ def test_infinity():
6262
INF /to/ 100
6363

6464
with raises(ValueError):
65-
NEGINF /to/ 1
65+
-INF /to/ 1
6666

6767
_assert_iter(1 /to/ INF /take/ 10, range(1, 11))
68-
_assert_iter(1 /to/ NEGINF /take/ 10, range(1, -9, -1))
68+
_assert_iter(1 /to/ -INF /take/ 10, range(1, -9, -1))
6969

7070
_assert_iter(1 /to/ INF /step/ 2 /take/ 10, list(range(1, 100, 2))[:10])
71-
_assert_iter(1 /to/ NEGINF /step/ 2 /take/ 10, list(range(1, -100, -2))[:10])
71+
_assert_iter(1 /to/ -INF /step/ 2 /take/ 10, list(range(1, -100, -2))[:10])
7272

73-
_assert_iter(1 /to/ NEGINF /step/ 2 /take/ 10 /drop/ 2, list(range(1, -100, -2))[2:10])
74-
_assert_iter(1 /to/ NEGINF /step/ 2 /drop/ 5 /take/ 3, [-9, -11, -13])
73+
_assert_iter(1 /to/ -INF /step/ 2 /take/ 10 /drop/ 2, list(range(1, -100, -2))[2:10])
74+
_assert_iter(1 /to/ -INF /step/ 2 /drop/ 5 /take/ 3, [-9, -11, -13])
7575

7676

7777
def test_take():

tests/test_iter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pytest import raises
22
from syntax_sugar.iter import Range, Iterator
3-
from syntax_sugar.infix import take, to, INF
3+
from syntax_sugar.util import take, to, INF
44
from itertools import product
55

66

0 commit comments

Comments
 (0)