From d9a5e3941803a1b128c17a4158b134aceb928a20 Mon Sep 17 00:00:00 2001 From: zyd Date: Thu, 26 May 2022 21:48:24 +0900 Subject: [PATCH] add methods repeat / times --- README.md | 4 ++-- ramda/__init__.py | 2 ++ ramda/repeat.py | 5 +++++ ramda/times.py | 20 ++++++++++++++++++++ test/test_repeat.py | 20 ++++++++++++++++++++ test/test_times.py | 24 ++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 ramda/repeat.py create mode 100644 ramda/times.py create mode 100644 test/test_repeat.py create mode 100644 test/test_times.py diff --git a/README.md b/README.md index 8047ad7..fce36f1 100644 --- a/README.md +++ b/README.md @@ -432,7 +432,7 @@ R.propEq(1, 'v1', {'v1': 1}) # True - [ ] reduceWhile - [x] 0.1.2 reject - [ ] remove -- [ ] repeat +- [x] repeat - [ ] replace - [x] 0.1.2 reverse - [ ] scan @@ -474,7 +474,7 @@ R.subtract(date(1,2,3), date(1,2,3)) # float('nan) - [x] 0.1.2 tap - [ ] test - [ ] thunkify -- [ ] times +- [x] times - [ ] toLower - [ ] toPairs - [ ] toPairsIn diff --git a/ramda/__init__.py b/ramda/__init__.py index a3bd500..43537d1 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -74,6 +74,7 @@ from .reduced import reduced from .reduceRight import reduceRight from .reject import reject +from .repeat import repeat from .reverse import reverse from .slice import slice from .sort import sort @@ -86,6 +87,7 @@ from .take import take from .takeWhile import takeWhile from .tap import tap +from .times import times from .toString import toString from .union import union from .unionWith import unionWith diff --git a/ramda/repeat.py b/ramda/repeat.py new file mode 100644 index 0000000..2d1d9d8 --- /dev/null +++ b/ramda/repeat.py @@ -0,0 +1,5 @@ +from .always import always +from .private._curry2 import _curry2 +from .times import times + +repeat = _curry2(lambda value, n: times(always(value), n)) diff --git a/ramda/times.py b/ramda/times.py new file mode 100644 index 0000000..e5d607b --- /dev/null +++ b/ramda/times.py @@ -0,0 +1,20 @@ +from math import isnan + +from .private._curry2 import _curry2 +from .private._helper import toNumber + + +def inner_times(fn, n): + length = toNumber(n) + idx = 0 + + if length < 0 or isnan(length): + raise ValueError('n must be a non-negative number') + arr = [] + while idx < length: + arr.append(fn(idx)) + idx += 1 + return arr + + +times = _curry2(inner_times) diff --git a/test/test_repeat.py b/test/test_repeat.py new file mode 100644 index 0000000..507f0f2 --- /dev/null +++ b/test/test_repeat.py @@ -0,0 +1,20 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/repeat.js +""" + + +class TestRepeat(unittest.TestCase): + def test_returns_a_lazy_list_of_identical_values(self): + self.assertEqual([0, 0, 0, 0, 0], R.repeat(0, 5)) + + def test_can_accept_any_value_including_None(self): + self.assertEqual([None, None, None], R.repeat(None, 3)) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_times.py b/test/test_times.py new file mode 100644 index 0000000..0de4ec1 --- /dev/null +++ b/test/test_times.py @@ -0,0 +1,24 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/times.js +""" + + +class TestTimes(unittest.TestCase): + def test_takes_a_map_func(self): + self.assertEqual([0, 1, 2, 3, 4], R.times(R.identity, 5)) + self.assertEqual([0, 2, 4, 6, 8], R.times(R.multiply(2), 5)) + + def test_throws_if_second_argument_is_not_a_valid_array_length(self): + with self.assertRaises(ValueError): + R.times(3)('cheers!') + with self.assertRaises(ValueError): + R.times(R.identity, -1) + + +if __name__ == '__main__': + unittest.main()