Skip to content

Commit e52d2c6

Browse files
authored
No longer rely on PyFunctional's seq.grouped which throws StopIteration (#221)
1 parent 9043775 commit e52d2c6

File tree

6 files changed

+32
-8
lines changed

6 files changed

+32
-8
lines changed

src/basilisp/compiler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from basilisp.lang.runtime import Var
3434
from basilisp.lang.typing import LispForm
3535
from basilisp.lang.util import genname, munge
36-
from basilisp.util import Maybe
36+
from basilisp.util import Maybe, partition
3737

3838
USE_VAR_INDIRECTION = 'use_var_indirection'
3939

@@ -621,7 +621,7 @@ def _assert_recur_is_tail(ctx: CompilerContext, form: lseq.Seq) -> None: # noqa
621621
except IndexError:
622622
pass
623623
elif child.first == _LET:
624-
for binding, val in seq(runtime.nth(child, 1)).grouped(2):
624+
for binding, val in partition(runtime.nth(child, 1), 2):
625625
_assert_no_recur(ctx, lseq.sequence([binding]))
626626
_assert_no_recur(ctx, lseq.sequence([val]))
627627
let_body = runtime.nthnext(child, 2)
@@ -1048,9 +1048,9 @@ def let_32(a_33, b_34, c_35):
10481048
# the variable names, which become Python locals in an expression context (which
10491049
# is a subtly different Python AST node); and the computed expressions.
10501050
st = ctx.symbol_table
1051-
bindings = seq(form[1]).grouped(2)
1051+
bindings = list(partition(form[1], 2))
10521052

1053-
if bindings.empty():
1053+
if not bindings:
10541054
raise CompilerException("Expected at least one binding in 'let*'") from None
10551055

10561056
arg_syms: Dict[

src/basilisp/lang/map.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from basilisp.lang.meta import Meta
1111
from basilisp.lang.seq import Seqable, sequence, Seq
1212
from basilisp.lang.util import lrepr
13+
from basilisp.util import partition
1314

1415

1516
class MapEntry:
@@ -121,7 +122,7 @@ def with_meta(self, meta: "Map") -> "Map":
121122

122123
def assoc(self, *kvs) -> "Map":
123124
m = self._inner.evolver()
124-
for k, v in seq(kvs).grouped(2):
125+
for k, v in partition(kvs, 2):
125126
m[k] = v
126127
return Map(m.persistent())
127128

@@ -199,5 +200,5 @@ def from_entries(entries):
199200

200201

201202
def hash_map(*pairs) -> Map:
202-
entries = seq(pairs).grouped(2).map(lambda v: MapEntry.of(v[0], v[1])).to_list()
203+
entries = seq(partition(pairs, 2)).map(lambda v: MapEntry.of(v[0], v[1])).to_list()
203204
return from_entries(entries)

src/basilisp/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,17 @@ def stream(self) -> Sequence:
8787
@property
8888
def is_present(self) -> bool:
8989
return self._inner is not None
90+
91+
92+
def partition(coll, n: int):
93+
"""Partition coll into groups of size n."""
94+
assert n > 0
95+
start = 0
96+
stop = n
97+
while stop <= len(coll):
98+
yield tuple(e for e in coll[start:stop])
99+
start += n
100+
stop += n
101+
if start < len(coll) < stop:
102+
stop = len(coll)
103+
yield tuple(e for e in coll[start:stop])

tests/basilisp/util_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from basilisp.util import partition
2+
3+
4+
def test_partition():
5+
assert [(1, 2, 3, 4)] == list(partition([1, 2, 3, 4], 5))
6+
assert [(1, 2, 3, 4)] == list(partition([1, 2, 3, 4], 4))
7+
assert [(1, 2, 3), (4,)] == list(partition([1, 2, 3, 4], 3))
8+
assert [(1, 2), (3, 4)] == list(partition([1, 2, 3, 4], 2))
9+
assert [(1,), (2,), (3,), (4,)] == list(partition([1, 2, 3, 4], 1))
File renamed without changes.

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[tox]
2-
envlist = py36,mypy,lint
2+
envlist = py36,py37,mypy,lint
33

44
[testenv]
55
deps =
66
six==1.10.0
77
commands =
8-
pytest --disable-warnings
8+
pytest
99

1010
[testenv:coverage]
1111
deps =

0 commit comments

Comments
 (0)