From 796dc189e9cf37ea186d459146c6050a2d517a67 Mon Sep 17 00:00:00 2001 From: zyd Date: Tue, 31 May 2022 21:46:03 +0900 Subject: [PATCH 1/3] add method nAry --- README.md | 2 +- ramda/__init__.py | 1 + ramda/nAry.py | 30 ++++++++++++++++++++++++++++++ test/test_nAry.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 ramda/nAry.py create mode 100644 test/test_nAry.py diff --git a/README.md b/README.md index 950273d..1f7abad 100644 --- a/README.md +++ b/README.md @@ -341,7 +341,7 @@ Python modulo on negative numbers has different behavior than JS. - [ ] move - [x] 0.1.2 multiply -- [ ] nAry +- [x] nAry - [ ] negate - [ ] none - [x] 0.1.2 not diff --git a/ramda/__init__.py b/ramda/__init__.py index 128e862..e9a7251 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -53,6 +53,7 @@ from .Min import Min from .modulo import modulo from .multiply import multiply +from .nAry import nAry from .Not import Not from .nth import nth from .objOf import objOf diff --git a/ramda/nAry.py b/ramda/nAry.py new file mode 100644 index 0000000..1053a34 --- /dev/null +++ b/ramda/nAry.py @@ -0,0 +1,30 @@ +from .private._curry2 import _curry2 + + +def inner_nAry(n, fn): + if n == 0: + return lambda *args: fn() + if n == 1: + return lambda a0=None, *args: fn(a0) + if n == 2: + return lambda a0=None, a1=None, *args: fn(a0, a1) + if n == 3: + return lambda a0=None, a1=None, a2=None, *args: fn(a0, a1, a2) + if n == 4: + return lambda a0=None, a1=None, a2=None, a3=None, *args: fn(a0, a1, a2, a3) + if n == 5: + return lambda a0=None, a1=None, a2=None, a3=None, a4=None, *args: fn(a0, a1, a2, a3, a4) + if n == 6: + return lambda a0=None, a1=None, a2=None, a3=None, a4=None, a5=None, *args: fn(a0, a1, a2, a3, a4, a5) + if n == 7: + return lambda a0=None, a1=None, a2=None, a3=None, a4=None, a5=None, a6=None, *args: fn(a0, a1, a2, a3, a4, a5, a6) + if n == 8: + return lambda a0=None, a1=None, a2=None, a3=None, a4=None, a5=None, a6=None, a7=None, *args: fn(a0, a1, a2, a3, a4, a5, a6, a7) + if n == 9: + return lambda a0=None, a1=None, a2=None, a3=None, a4=None, a5=None, a6=None, a7=None, a8=None, *args: fn(a0, a1, a2, a3, a4, a5, a6, a7, a8) + if n == 10: + return lambda a0=None, a1=None, a2=None, a3=None, a4=None, a5=None, a6=None, a7=None, a8=None, a9=None, *args: fn(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) + raise ValueError('First argument to nAry must be a non-negative integer no greater than ten') + + +nAry = _curry2(inner_nAry) diff --git a/test/test_nAry.py b/test/test_nAry.py new file mode 100644 index 0000000..8cfedd5 --- /dev/null +++ b/test/test_nAry.py @@ -0,0 +1,42 @@ + +import unittest + +import ramda as R +from ramda.private._inspect import funcArgsLength + +""" +https://github.com/ramda/ramda/blob/master/test/nAry.js +""" + + +def toArray(*args): + return list(args) + + +class TestNAry(unittest.TestCase): + def test_turns_multiple_argument_function_into_a_nullary_one(self): + fn = R.nAry(0, toArray) + self.assertEqual(0, funcArgsLength(fn)) + self.assertEqual([], fn(1, 2, 3)) + + def test_turns_multiple_argument_function_into_a_ternary_one(self): + fn = R.nAry(3, toArray) + self.assertEqual(3, funcArgsLength(fn)) + self.assertEqual([1, 2, 3], fn(1, 2, 3, 4)) + self.assertEqual([1, None, None], fn(1)) + + def test_creates_functions_of_arity_less_than_or_equal_to_ten(self): + fn = R.nAry(10, toArray) + self.assertEqual(10, funcArgsLength(fn)) + self.assertEqual(R.range(0, 10), fn(*R.range(0, 25))) + + nones = fn() + ns = R.repeat(None, 10) + self.assertEqual(nones, ns) + + def test_throws_if_n_is_greater_than_ten(self): + with self.assertRaises(ValueError): + R.nAry(11, toArray) + +if __name__ == '__main__': + unittest.main() From 5715053434c01cf5d5c46e82bbe67025192456fb Mon Sep 17 00:00:00 2001 From: zyd Date: Wed, 1 Jun 2022 13:12:54 +0900 Subject: [PATCH 2/3] increase nAry coverage --- test/test_nAry.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/test_nAry.py b/test/test_nAry.py index 8cfedd5..11538fd 100644 --- a/test/test_nAry.py +++ b/test/test_nAry.py @@ -38,5 +38,32 @@ def test_throws_if_n_is_greater_than_ten(self): with self.assertRaises(ValueError): R.nAry(11, toArray) + def test_all_arity(self): + self.assertEqual(0, funcArgsLength(R.nAry(0, toArray))) + self.assertEqual(1, funcArgsLength(R.nAry(1, toArray))) + self.assertEqual(2, funcArgsLength(R.nAry(2, toArray))) + self.assertEqual(3, funcArgsLength(R.nAry(3, toArray))) + self.assertEqual(4, funcArgsLength(R.nAry(4, toArray))) + self.assertEqual(5, funcArgsLength(R.nAry(5, toArray))) + self.assertEqual(6, funcArgsLength(R.nAry(6, toArray))) + self.assertEqual(7, funcArgsLength(R.nAry(7, toArray))) + self.assertEqual(8, funcArgsLength(R.nAry(8, toArray))) + self.assertEqual(9, funcArgsLength(R.nAry(9, toArray))) + self.assertEqual(10, funcArgsLength(R.nAry(10, toArray))) + + input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] + self.assertEqual([], R.nAry(0, toArray)(*input)) + self.assertEqual([1], R.nAry(1, toArray)(*input)) + self.assertEqual([1, 2], R.nAry(2, toArray)(*input)) + self.assertEqual([1, 2, 3], R.nAry(3, toArray)(*input)) + self.assertEqual([1, 2, 3, 4], R.nAry(4, toArray)(*input)) + self.assertEqual([1, 2, 3, 4, 5], R.nAry(5, toArray)(*input)) + self.assertEqual([1, 2, 3, 4, 5, 6], R.nAry(6, toArray)(*input)) + self.assertEqual([1, 2, 3, 4, 5, 6, 7], R.nAry(7, toArray)(*input)) + self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8], R.nAry(8, toArray)(*input)) + self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9], R.nAry(9, toArray)(*input)) + self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], R.nAry(10, toArray)(*input)) + + if __name__ == '__main__': unittest.main() From 02b450c276a159e37a0f91db34e62555d7b2e7a1 Mon Sep 17 00:00:00 2001 From: zyd Date: Wed, 1 Jun 2022 13:23:03 +0900 Subject: [PATCH 3/3] add method binary --- README.md | 2 +- ramda/__init__.py | 1 + ramda/binary.py | 4 ++++ test/test_binary.py | 20 ++++++++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 ramda/binary.py create mode 100644 test/test_binary.py diff --git a/README.md b/README.md index 1f7abad..1a3263e 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ R.add(date(1,2,3), date(1,2,3)) # float('nan) - [ ] ascend - [ ] assoc - [ ] assocPath -- [ ] binary +- [x] binary - [ ] bind - [ ] both - [ ] call diff --git a/ramda/__init__.py b/ramda/__init__.py index e9a7251..71aad9e 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -6,6 +6,7 @@ from .And import And from .any import any from .append import append +from .binary import binary from .clone import clone from .comparator import comparator from .compose import compose diff --git a/ramda/binary.py b/ramda/binary.py new file mode 100644 index 0000000..d5e0fae --- /dev/null +++ b/ramda/binary.py @@ -0,0 +1,4 @@ +from .nAry import nAry +from .private._curry1 import _curry1 + +binary = _curry1(lambda fn: nAry(2, fn)) diff --git a/test/test_binary.py b/test/test_binary.py new file mode 100644 index 0000000..7f67cfa --- /dev/null +++ b/test/test_binary.py @@ -0,0 +1,20 @@ + +import unittest + +import ramda as R +from ramda.private._inspect import funcArgsLength + +""" +https://github.com/ramda/ramda/blob/master/test/binary.js +""" + + +class TestBinary(unittest.TestCase): + def test_turns_multiple_argument_function_into_binary_one(self): + fn = R.binary(lambda *args: list(args)) + self.assertEqual(2, funcArgsLength(fn)) + self.assertEqual([10, 20], fn(10, 20)) + + +if __name__ == '__main__': + unittest.main()