Skip to content

Commit e6aa440

Browse files
Merge pull request #10 from brandonwillard/updates-for-stream-unification
Update unification extension for stream-processing
2 parents 5d12759 + d886d5d commit e6aa440

File tree

4 files changed

+28
-33
lines changed

4 files changed

+28
-33
lines changed

cons/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def cons_merge(cls, car_part, cdr_part):
122122
return chain((car_part,), cdr_part)
123123

124124
def __hash__(self):
125-
return hash([self.car, self.cdr])
125+
return hash((self.car, self.cdr))
126126

127127
def __eq__(self, other):
128128
return (

cons/unify.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
from itertools import tee
2-
from collections import OrderedDict
32
from collections.abc import Iterator, Mapping
43

5-
from unification.core import unify, _unify, reify, _reify
4+
from unification.core import (
5+
_unify,
6+
_reify,
7+
construction_sentinel,
8+
)
69

710
from .core import car, cdr, ConsPair, cons, MaybeCons, ConsError
811

912

10-
def _cons_unify(lcons, rcons, s):
13+
def _unify_Cons(lcons, rcons, s):
1114

1215
lcons_ = lcons
1316
rcons_ = rcons
@@ -19,27 +22,23 @@ def _cons_unify(lcons, rcons, s):
1922
rcons, rcons_ = tee(rcons)
2023

2124
try:
22-
s = unify(car(lcons), car(rcons), s)
25+
s = yield _unify(car(lcons), car(rcons), s)
2326

2427
if s is not False:
25-
return unify(cdr(lcons_), cdr(rcons_), s)
28+
s = yield _unify(cdr(lcons_), cdr(rcons_), s)
2629
except ConsError:
27-
pass
30+
yield False
31+
else:
32+
yield s
2833

29-
return False
3034

31-
32-
_unify.add((ConsPair, (ConsPair, MaybeCons), Mapping), _cons_unify)
33-
_unify.add((MaybeCons, ConsPair, Mapping), _cons_unify)
34-
35-
36-
@_reify.register(OrderedDict, Mapping)
37-
def reify_OrderedDict(od, s):
38-
return OrderedDict((k, reify(v, s)) for k, v in od.items())
35+
_unify.add((ConsPair, (ConsPair, MaybeCons), Mapping), _unify_Cons)
36+
_unify.add((MaybeCons, ConsPair, Mapping), _unify_Cons)
3937

4038

4139
@_reify.register(ConsPair, Mapping)
42-
def reify_cons(lcons, s):
43-
rcar = reify(car(lcons), s)
44-
rcdr = reify(cdr(lcons), s)
45-
return cons(rcar, rcdr)
40+
def _reify_Cons(lcons, s):
41+
rcar = yield _reify(car(lcons), s)
42+
rcdr = yield _reify(cdr(lcons), s)
43+
yield construction_sentinel
44+
yield cons(rcar, rcdr)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
name="cons",
1010
version=versioneer.get_version(),
1111
cmdclass=versioneer.get_cmdclass(),
12-
install_requires=["logical-unification"],
12+
install_requires=["logical-unification>=0.4.0"],
1313
packages=find_packages(exclude=["tests"]),
1414
tests_require=["pytest"],
1515
author="Brandon T. Willard",

tests/test_cons.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22

3-
from functools import reduce
43
from itertools import chain, cycle
54
from collections import OrderedDict, UserList
65
from collections.abc import Iterator
@@ -11,14 +10,6 @@
1110
from cons.core import ConsPair, MaybeCons, ConsNull, ConsError, NonCons
1211

1312

14-
def assert_all_equal(*tests):
15-
def _equal(x, y):
16-
assert x, y
17-
return y
18-
19-
reduce(_equal, tests)
20-
21-
2213
def test_noncons_type():
2314

2415
with pytest.raises(TypeError):
@@ -83,8 +74,7 @@ def test_cons_join():
8374
cons("a")
8475

8576
assert cons(1, 2, 3, 4) == cons(1, cons(2, cons(3, 4)))
86-
87-
assert_all_equal(cons("a", None), cons("a", []), ["a"])
77+
assert cons("a", None) == cons("a", []) == ["a"]
8878
assert cons("a", ()) == ("a",)
8979
assert cons("a", []) == ["a"]
9080
assert cons(None, "a").car is None
@@ -124,7 +114,9 @@ def __add__(self, a):
124114
assert cons(1, CustomList([2, 3])) is clist_res
125115

126116

127-
def test_cons_str():
117+
def test_cons_class():
118+
c = cons(1, 2)
119+
assert {c: 1}[c] == 1
128120
assert repr(cons(1, 2)) == "ConsPair(1, 2)"
129121
assert str(cons(1, 2, 3)) == "(1 . (2 . 3))"
130122

@@ -257,6 +249,10 @@ def test_unification():
257249
assert res[car_lv] == 1
258250
assert list(res[cdr_lv]) == []
259251

252+
res = unify(cons(car_lv, cdr_lv), iter([1]), {})
253+
assert res[car_lv] == 1
254+
assert list(res[cdr_lv]) == []
255+
260256
res = unify(OrderedDict([("a", 1), ("b", 2)]), cons(car_lv, cdr_lv), {})
261257
assert res[car_lv] == ("a", 1)
262258
assert res[cdr_lv] == [("b", 2)]

0 commit comments

Comments
 (0)