Skip to content

Commit

Permalink
Implementation for fixing issue #124
Browse files Browse the repository at this point in the history
  • Loading branch information
EntilZha committed Apr 1, 2018
1 parent f982685 commit 8d5757f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions functional/test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,11 @@ def test_partition(self):
self.assert_type(p1)
self.assert_type(p2)

result = self.seq([[1, 2, 3], [4, 5, 6]]).flatten().partition(lambda x: x > 2)
expect = [[3, 4, 5, 6], [1, 2]]
self.assertIteratorEqual(expect, list(result))
self.assert_type(result)

def test_cartesian(self):
result = seq.range(3).cartesian(range(3)).list()
self.assertListEqual(result, list(product(range(3), range(3))))
Expand Down
16 changes: 13 additions & 3 deletions functional/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,18 @@ def sliding_t(wrap, size, step):
)


def partition_impl(wrap, predicate, sequence):
truthy_partition = []
falsy_partition = []
for e in sequence:
if predicate(e):
truthy_partition.append(e)
else:
falsy_partition.append(e)

return wrap((wrap(truthy_partition), wrap(falsy_partition)))


def partition_t(wrap, func):
"""
Transformation for Sequence.partition
Expand All @@ -668,9 +680,7 @@ def partition_t(wrap, func):
"""
return Transformation(
'partition({0})'.format(name(func)),
lambda sequence: wrap(
(wrap(filter(func, sequence)), wrap(filter(lambda val: not func(val), sequence)))
),
partial(partition_impl, wrap, func),
None
)

Expand Down

0 comments on commit 8d5757f

Please sign in to comment.