Skip to content

Commit 56ff25b

Browse files
Initial Fix for Issue 158 (#160)
Elements that are lists in a sequence will remain lists when grouped. Additionally, cleaning up as per the suggestions of black.
1 parent 6c50426 commit 56ff25b

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

functional/pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ def grouped(self, size):
12661266
:param size: size of the partitions
12671267
:return: sequence partitioned into groups of length size
12681268
"""
1269-
return self._transform(transformations.grouped_t(_wrap, size))
1269+
return self._transform(transformations.grouped_t(size))
12701270

12711271
def sliding(self, size, step=1):
12721272
"""

functional/test/test_functional.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,38 @@ def test_grouped_returns_list(self):
619619
self.assertTrue(is_iterable(l.grouped(2)))
620620
self.assertTrue(is_iterable(l.grouped(3)))
621621

622+
def test_grouped_returns_list_of_lists(self):
623+
test_inputs = [
624+
[i for i in "abcdefghijklmnop"],
625+
[None for i in range(10)],
626+
[i for i in range(10)],
627+
[[i] for i in range(10)],
628+
[{i} for i in range(10)],
629+
[{i, i + 1} for i in range(10)],
630+
[[i, i + 1] for i in range(10)],
631+
]
632+
633+
def gen_test(collection, group_size):
634+
expected_type = type(collection[0])
635+
636+
types_after_grouping = (
637+
seq(collection)
638+
.grouped(group_size)
639+
.flatten()
640+
.map(lambda item: type(item))
641+
)
642+
643+
err_msg = f"Typing was not maintained after grouping. An input of {collection} yielded output types of {set(types_after_grouping)} and not {expected_type} as expected."
644+
645+
return types_after_grouping.for_all(lambda t: t == expected_type), err_msg
646+
647+
for test_input in test_inputs:
648+
for group_size in [1, 2, 4, 7]:
649+
all_sub_collections_are_lists, err_msg = gen_test(
650+
test_input, group_size
651+
)
652+
self.assertTrue(all_sub_collections_are_lists, msg=err_msg)
653+
622654
def test_sliding(self):
623655
l = self.seq([1, 2, 3, 4, 5, 6, 7])
624656
expect = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]

functional/transformations.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,9 @@ def group_by_t(func):
564564
)
565565

566566

567-
def grouped_impl(wrap, size, sequence):
567+
def grouped_impl(size, sequence):
568568
"""
569569
Implementation for grouped_t
570-
:param wrap: wrap children values with this
571570
:param size: size of groups
572571
:param sequence: sequence to group
573572
:return: grouped sequence
@@ -576,20 +575,19 @@ def grouped_impl(wrap, size, sequence):
576575
try:
577576
while True:
578577
batch = islice(iterator, size)
579-
yield list(chain((wrap(next(batch)),), batch))
578+
yield list(chain((next(batch),), batch))
580579
except StopIteration:
581580
return
582581

583582

584-
def grouped_t(wrap, size):
583+
def grouped_t(size):
585584
"""
586585
Transformation for Sequence.grouped
587-
:param wrap: wrap children values with this
588586
:param size: size of groups
589587
:return: transformation
590588
"""
591589
return Transformation(
592-
"grouped({0})".format(size), partial(grouped_impl, wrap, size), None
590+
"grouped({0})".format(size), partial(grouped_impl, size), None
593591
)
594592

595593

0 commit comments

Comments
 (0)