diff --git a/README.md b/README.md index 23c2400..56bdc5d 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,7 @@ R.length(ObjWithoutLength()) # float('nan') - [ ] mapAccumRight - [ ] mapObjIndexed - [x] 0.1.2 match -- [ ] mathMod +- [x] mathMod - [x] 0.1.2 Max (`max` is a keyword in python) If R.Max(a, b) diff --git a/ramda/__init__.py b/ramda/__init__.py index d8f2dee..a71cdf8 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -54,6 +54,7 @@ from .lte import lte from .map import map from .match import match +from .mathMod import mathMod from .Max import Max from .Min import Min from .modulo import modulo diff --git a/ramda/mathMod.py b/ramda/mathMod.py new file mode 100644 index 0000000..2ef5c2b --- /dev/null +++ b/ramda/mathMod.py @@ -0,0 +1,13 @@ +from .private._curry2 import _curry2 +from .private._isInteger import _isInteger + + +def inner_mathMod(m, p): + if not _isInteger(m): + return float('nan') + if not _isInteger(p) or p < 1: + return float('nan') + return m % p + + +mathMod = _curry2(inner_mathMod) diff --git a/test/test_mathMod.py b/test/test_mathMod.py new file mode 100644 index 0000000..4512ece --- /dev/null +++ b/test/test_mathMod.py @@ -0,0 +1,25 @@ + +import unittest +from math import isnan + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/mathMod.js +""" + + +class TestMathMod(unittest.TestCase): + def test_requires_integer_arguments(self): + self.assertTrue(isnan(R.mathMod('s', 3))) + self.assertTrue(isnan(R.mathMod(3, 's'))) + self.assertTrue(isnan(R.mathMod(12.2, 3))) + self.assertTrue(isnan(R.mathMod(3, 12.2))) + + def test_computes_the_true_modulo_function(self): + self.assertEqual(3, R.mathMod(-17, 5)) + self.assertEqual(2, R.mathMod(17, 5)) + self.assertEqual(3, R.mathMod(15, 12)) + +if __name__ == '__main__': + unittest.main()