Skip to content

Commit

Permalink
Implemented cartesian product
Browse files Browse the repository at this point in the history
  • Loading branch information
EntilZha committed Jun 4, 2016
1 parent 66f1fb4 commit c19c4a2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Implemented `pseq` by implementing `ParallelStream` and `ParallelExecutionEngine`
* `map`, `select`, `filter`, `filter_not`, `where`, `flatten`, and `flat_map` parallelized
* Many Thanks to `versae` for implementing the `pseq` feature!
* Cartesian product from `itertools.product` implemented as `cartesian`

## Release 0.6.0
### New Features
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ Function | Description | Type
`zip(other)` | Zips the sequence with `other` | transformation
`zip_with_index(start=0)` | Zips the sequence with the index starting at `start` on the right side | transformation
`enumerate(start=0)` | Zips the sequence with the index starting at `start` on the left side | transformation
`cartesian(*iterables, repeat=1)` | Returns cartesian product from itertools.product | transformation
`inner_join(other)` | Returns inner join of sequence with other. Must be a sequence of `(key, value)` pairs | transformation
`outer_join(other)` | Returns outer join of sequence with other. Must be a sequence of `(key, value)` pairs | transformation
`left_join(other)` | Returns left join of sequence with other. Must be a sequence of `(key, value)` pairs | transformation
Expand Down
12 changes: 12 additions & 0 deletions functional/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,18 @@ def tails(self):
"""
return self._transform(transformations.tails_t(_wrap))

def cartesian(self, *iterables, **kwargs):
"""
Returns the cartesian product of the passed iterables with the specified number of
repetitions.
The keyword argument `repeat` is read from kwargs to pass to itertools.cartesian.
:param iterables: elements for cartesian product
:return: cartesian product
"""
return self._transform(transformations.cartesian_t(iterables, kwargs.get('repeat', 1)))

def drop(self, n):
"""
Drop the first n elements of the sequence.
Expand Down
12 changes: 12 additions & 0 deletions functional/test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
import unittest
from collections import namedtuple
from itertools import product

from functional.pipeline import Sequence, is_iterable, _wrap
from functional.transformations import name
from functional import seq, pseq
Expand Down Expand Up @@ -604,6 +606,16 @@ def test_partition(self):
self.assert_type(p1)
self.assert_type(p2)

def test_cartesian(self):
result = seq.range(3).cartesian(range(3)).list()
self.assertListEqual(result, list(product(range(3), range(3))))

result = seq.range(3).cartesian(range(3), range(2)).list()
self.assertListEqual(result, list(product(range(3), range(3), range(2))))

result = seq.range(3).cartesian(range(3), range(2), repeat=2).list()
self.assertListEqual(result, list(product(range(3), range(3), range(2), repeat=2)))

def test_product(self):
l = [2, 2, 3]
self.assertEqual(12, self.seq(l).product())
Expand Down
16 changes: 15 additions & 1 deletion functional/transformations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import absolute_import

from functools import reduce, partial
from itertools import dropwhile, takewhile, islice, count
from itertools import dropwhile, takewhile, islice, count, product
import collections
import types

Expand Down Expand Up @@ -306,6 +306,20 @@ def enumerate_t(start):
)


def cartesian_t(iterables, repeat):
"""
Transformation for Sequence.cartesian
:param iterables: elements for cartesian product
:param repeat: how many times to repeat iterables
:return: transformation
"""
return Transformation(
'cartesian',
lambda sequence: product(sequence, *iterables, repeat=repeat),
None
)


def init_t():
"""
Transformation for Sequence.init
Expand Down

0 comments on commit c19c4a2

Please sign in to comment.