diff --git a/README.md b/README.md index e450739..115e53d 100644 --- a/README.md +++ b/README.md @@ -512,7 +512,7 @@ we will compare with str(a) and str(b). R.Min('A', None) # 'A', 'A' < 'None' ``` -- [ ] minBy +- [x] minBy - [ ] modify - [ ] modifyPath - [x] 0.1.4 modulo diff --git a/ramda/__init__.py b/ramda/__init__.py index 085e071..f053ce1 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -79,6 +79,7 @@ from .mathMod import mathMod from .Max import Max from .Min import Min +from .minBy import minBy from .modulo import modulo from .multiply import multiply from .nAry import nAry diff --git a/ramda/minBy.py b/ramda/minBy.py new file mode 100644 index 0000000..eee1656 --- /dev/null +++ b/ramda/minBy.py @@ -0,0 +1,10 @@ +from .Min import Min +from .private._curry3 import _curry3 + + +def inner_minBy(f, a, b): + resultB = f(b) + return b if Min(f(a), resultB) == resultB else a + + +minBy = _curry3(inner_minBy) diff --git a/test/test_minBy.py b/test/test_minBy.py new file mode 100644 index 0000000..757119c --- /dev/null +++ b/test/test_minBy.py @@ -0,0 +1,18 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/minBy.js +""" + + +class TestMinBy(unittest.TestCase): + def test_returns_the_smaller_value_as_determined_by_the_function(self): + self.assertEqual(2, R.minBy(lambda x: x * x, -3, 2)) + self.assertEqual({'x': 3, 'y': 1}, R.minBy(R.prop('x'), {'x': 3, 'y': 1}, {'x': 5, 'y': 10})) + + +if __name__ == '__main__': + unittest.main()