diff --git a/README.md b/README.md index 544dbfa..8047ad7 100644 --- a/README.md +++ b/README.md @@ -304,7 +304,18 @@ R.Min('A', None) # 'A', 'A' < 'None' - [ ] minBy - [ ] modify - [ ] modifyPath -- [ ] modulo +- [x] modulo + +Python modulo on negative numbers has different behavior than JS. + +```python +5 % -3 # -1 +``` + +```js +5 % -3 // 2 +``` + - [ ] move - [x] 0.1.2 multiply - [ ] nAry diff --git a/ramda/__init__.py b/ramda/__init__.py index 7ca82f9..a3bd500 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -48,6 +48,7 @@ from .match import match from .Max import Max from .Min import Min +from .modulo import modulo from .multiply import multiply from .Not import Not from .nth import nth diff --git a/ramda/modulo.py b/ramda/modulo.py new file mode 100644 index 0000000..1cdc4b9 --- /dev/null +++ b/ramda/modulo.py @@ -0,0 +1,3 @@ +from .private._curry2 import _curry2 + +modulo = _curry2(lambda x, y: x % y) diff --git a/test/test_modulo.py b/test/test_modulo.py new file mode 100644 index 0000000..ce659b2 --- /dev/null +++ b/test/test_modulo.py @@ -0,0 +1,22 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/modulo.js +""" + + +class TestModulo(unittest.TestCase): + def test_divedes_the_first_param_by_the_second_and_returns_the_remainder(self): + self.assertEqual(0, R.modulo(100, 2)) + self.assertEqual(1, R.modulo(100, 3)) + self.assertEqual(15, R.modulo(100, 17)) + + def test_preserves_python_style_modulo_evaluation_for_negative_numbers(self): + self.assertEqual(3, R.modulo(-5, 4)) + + +if __name__ == '__main__': + unittest.main()