Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,6 @@ R.values({'a': 1, 'b': 2}) # [1, 2]
- [ ] without
- [ ] xor
- [x] xprod
- [ ] zip
- [x] zip
- [ ] zipObj
- [ ] zipWith
- [x] zipWith
3 changes: 3 additions & 0 deletions ramda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions ramda/zip.py
Original file line number Diff line number Diff line change
@@ -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]))
14 changes: 14 additions & 0 deletions ramda/zipWith.py
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions test/test_zip.py
Original file line number Diff line number Diff line change
@@ -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()
27 changes: 27 additions & 0 deletions test/test_zipWith.py
Original file line number Diff line number Diff line change
@@ -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()