diff --git a/README.md b/README.md index ae42946..60506e2 100644 --- a/README.md +++ b/README.md @@ -515,6 +515,6 @@ R.values({'a': 1, 'b': 2}) # [1, 2] - [ ] without - [ ] xor - [x] xprod -- [ ] zip +- [x] zip - [ ] zipObj -- [ ] zipWith +- [x] zipWith diff --git a/ramda/__init__.py b/ramda/__init__.py index 132c0dd..6c1d97b 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -90,3 +90,6 @@ from .useWith import useWith from .values import values from .xprod import xprod +# pylint: disable=redefined-builtin +from .zip import zip +from .zipWith import zipWith diff --git a/ramda/zip.py b/ramda/zip.py new file mode 100644 index 0000000..11d16ba --- /dev/null +++ b/ramda/zip.py @@ -0,0 +1,7 @@ +from ramda import zipWith + +from .private._curry2 import _curry2 +from .zipWith import zipWith + +# pylint: disable=redefined-builtin +zip = _curry2(zipWith(lambda a, b: [a, b])) diff --git a/ramda/zipWith.py b/ramda/zipWith.py new file mode 100644 index 0000000..5859598 --- /dev/null +++ b/ramda/zipWith.py @@ -0,0 +1,14 @@ +from .private._curry3 import _curry3 + + +def inner_zipWith(fn, a, b): + rv = [] + idx = 0 + length = min(len(a), len(b)) + while idx < length: + rv.append(fn(a[idx], b[idx])) + idx += 1 + return rv + + +zipWith = _curry3(inner_zipWith) diff --git a/test/test_zip.py b/test/test_zip.py new file mode 100644 index 0000000..d39b290 --- /dev/null +++ b/test/test_zip.py @@ -0,0 +1,25 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/zip.js +""" + + +class TestZip(unittest.TestCase): + def test_returns_an_array_of_tuples(self): + a = [1, 2, 3] + b = [100, 200, 300] + self.assertEqual([[1, 100], [2, 200], [3, 300]], R.zip(a, b)) + + def test_returns_a_list_as_long_as_the_shorter_of_the_lists_input(self): + a = [1, 2, 3] + b = [100, 200, 300, 400] + c = [10, 20] + self.assertEqual([[1, 100], [2, 200], [3, 300]], R.zip(a, b)) + self.assertEqual([[1, 10], [2, 20]], R.zip(a, c)) + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_zipWith.py b/test/test_zipWith.py new file mode 100644 index 0000000..0ffc091 --- /dev/null +++ b/test/test_zipWith.py @@ -0,0 +1,27 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/zipWith.js +""" + +a = [1, 2, 3] +b = [100, 200, 300] +c = [10, 20, 30, 40, 50, 60] +def s(a, b): return f'{a} cow {b}' + + +class TestZipWith(unittest.TestCase): + def test_returns_an_array_created_by_applying_its_passed_in_function_pair_wise_on_its_passed_in_arrays(self): + self.assertEqual([101, 202, 303], R.zipWith(R.add, a, b)) + self.assertEqual([100, 400, 900], R.zipWith(R.multiply, a, b)) + self.assertEqual(['1 cow 100', '2 cow 200', '3 cow 300'], R.zipWith(s, a, b)) + + def test_returns_an_array_whose_length_is_equal_to_the_shorter_of_its_input_arrays(self): + self.assertEqual(len(a), len(R.zipWith(R.add, a, c))) + + +if __name__ == '__main__': + unittest.main()