diff --git a/README.md b/README.md index 6f8bc47..ae42946 100644 --- a/README.md +++ b/README.md @@ -514,7 +514,7 @@ R.values({'a': 1, 'b': 2}) # [1, 2] - [ ] whereEq - [ ] without - [ ] xor -- [ ] xprod +- [x] xprod - [ ] zip - [ ] zipObj - [ ] zipWith diff --git a/ramda/__init__.py b/ramda/__init__.py index 1e26a0d..132c0dd 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -89,3 +89,4 @@ from .uniqWith import uniqWith from .useWith import useWith from .values import values +from .xprod import xprod diff --git a/ramda/xprod.py b/ramda/xprod.py new file mode 100644 index 0000000..4874a2a --- /dev/null +++ b/ramda/xprod.py @@ -0,0 +1,18 @@ +from .private._curry2 import _curry2 + + +def inner_xprod(a, b): + idx = 0 + ilen = len(a) + jlen = len(b) + result = [] + while idx < ilen: + j = 0 + while j < jlen: + result.append([a[idx], b[j]]) + j += 1 + idx += 1 + return result + + +xprod = _curry2(inner_xprod) diff --git a/test/test_xprod.py b/test/test_xprod.py new file mode 100644 index 0000000..6558cab --- /dev/null +++ b/test/test_xprod.py @@ -0,0 +1,21 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/xprod.js +""" + + +class TestXProd(unittest.TestCase): + def test_returns_an_empty_list_if_either_input_list_is_empty(self): + self.assertEqual([], R.xprod([], [1, 2, 3])) + self.assertEqual([], R.xprod([1, 2, 3], [])) + + def test_creates_the_collection_of_all_cross_product_pairs_of_its_parameters(self): + self.assertEqual([[1, 'a'], [1, 'b'], [1, 'c'], [2, 'a'], [2, 'b'], [2, 'c']], R.xprod([1, 2], ['a', 'b', 'c'])) + + +if __name__ == '__main__': + unittest.main()