From afc1a46306a5b0c73a153649d02ca9ee639eb97d Mon Sep 17 00:00:00 2001 From: zyd Date: Sun, 21 Aug 2022 08:59:59 +0900 Subject: [PATCH] add method maxBy --- README.md | 2 +- ramda/__init__.py | 1 + ramda/maxBy.py | 10 ++++++++++ test/test_maxBy.py | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 ramda/maxBy.py create mode 100644 test/test_maxBy.py diff --git a/README.md b/README.md index e450739..cf1682e 100644 --- a/README.md +++ b/README.md @@ -489,7 +489,7 @@ we will compare with str(a) and str(b). R.Max('A', None) # None, 'A' < 'None' ``` -- [ ] maxBy +- [x] maxBy - [ ] mean - [ ] median - [ ] memoizeWith diff --git a/ramda/__init__.py b/ramda/__init__.py index 085e071..c6c4a8f 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -78,6 +78,7 @@ from .match import match from .mathMod import mathMod from .Max import Max +from .maxBy import maxBy from .Min import Min from .modulo import modulo from .multiply import multiply diff --git a/ramda/maxBy.py b/ramda/maxBy.py new file mode 100644 index 0000000..90a7450 --- /dev/null +++ b/ramda/maxBy.py @@ -0,0 +1,10 @@ +from .Max import Max +from .private._curry3 import _curry3 + + +def inner_maxBy(f, a, b): + resultB = f(b) + return b if Max(f(a), resultB) == resultB else a + + +maxBy = _curry3(inner_maxBy) diff --git a/test/test_maxBy.py b/test/test_maxBy.py new file mode 100644 index 0000000..02e463c --- /dev/null +++ b/test/test_maxBy.py @@ -0,0 +1,17 @@ +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/maxBy.js +""" + + +class TestMaxBy(unittest.TestCase): + def test_returns_the_larger_value_as_determined_by_the_function(self): + self.assertEqual(-3, R.maxBy(lambda x: x * x, -3, 2)) + self.assertEqual({'x': 5, 'y': 10}, R.maxBy(R.prop('x'), {'x': 3, 'y': 1}, {'x': 5, 'y': 10})) + + +if __name__ == '__main__': + unittest.main()