Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method maxBy #177

Merged
merged 1 commit into from
Aug 23, 2022
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ramda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions ramda/maxBy.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions test/test_maxBy.py
Original file line number Diff line number Diff line change
@@ -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()