From 96fd47ae33f082db837475dd3a1019cdeaf0a199 Mon Sep 17 00:00:00 2001 From: zyd Date: Tue, 19 Jul 2022 16:36:29 +0900 Subject: [PATCH] add method lift --- README.md | 2 +- ramda/__init__.py | 1 + ramda/lift.py | 5 +++++ test/test_lift.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 ramda/lift.py create mode 100644 test/test_lift.py diff --git a/README.md b/README.md index a82b0da..8e89072 100644 --- a/README.md +++ b/README.md @@ -443,7 +443,7 @@ R.length(ObjWithoutLength()) # float('nan') - [ ] lensIndex - [ ] lensPath - [ ] lensProp -- [ ] lift +- [x] lift - [x] liftN - [x] 0.1.2 lt - [x] 0.1.2 lte diff --git a/ramda/__init__.py b/ramda/__init__.py index 06d1763..eef3dde 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -60,6 +60,7 @@ from .last import last from .lastIndexOf import lastIndexOf from .length import length +from .lift import lift from .liftN import liftN from .lt import lt from .lte import lte diff --git a/ramda/lift.py b/ramda/lift.py new file mode 100644 index 0000000..49bd221 --- /dev/null +++ b/ramda/lift.py @@ -0,0 +1,5 @@ +from .liftN import liftN +from .private._curry1 import _curry1 +from .private._inspect import funcArgsLength + +lift = _curry1(lambda fn: liftN(funcArgsLength(fn), fn)) diff --git a/test/test_lift.py b/test/test_lift.py new file mode 100644 index 0000000..b610b35 --- /dev/null +++ b/test/test_lift.py @@ -0,0 +1,42 @@ + +import unittest + +import ramda as R +from ramda.private._isFunction import _isFunction + +from .helpers.Maybe import Just + +""" +https://github.com/ramda/ramda/blob/master/test/lift.js +""" + +add3 = R.curry(lambda a, b, c: a + b + c) +add4 = R.curry(lambda a, b, c, d: a + b + c + d) +add5 = R.curry(lambda a, b, c, d, e: a + b + c + d + e) +complement = R.lift(R.Not) +madd3 = R.lift(add3) +madd4 = R.lift(add4) +madd5 = R.lift(add5) + + +class TestLift(unittest.TestCase): + def test_returns_a_function_if_called_with_just_a_function(self): + self.assertEqual(True, _isFunction(R.lift(R.add))) + + def test_produces_a_cross_product_of_array_values(self): + self.assertEqual([3, 4, 5, 4, 5, 6, 4, 5, 6, 5, 6, 7, 5, 6, 7, 6, 7, 8], madd3([1, 2, 3], [1, 2], [1, 2, 3])) + self.assertEqual([6], madd3([1], [2], [3])) + self.assertEqual([9, 10, 10, 11, 10, 11, 11, 12], madd3([1, 2], [3, 4], [5, 6])) + + def test_can_lift_functions_of_any_arity(self): + self.assertEqual(False, complement(R.isNil)(None)) + self.assertEqual([6, 15], madd3([1, 10], [2], [3])) + self.assertEqual([46, 55], madd4([1, 10], [2], [3], [40])) + self.assertEqual([546, 1046, 555, 1055], madd5([1, 10], [2], [3], [40], [500, 1000])) + + def test_works_with_other_functors_such_as_maybe(self): + addM = R.lift(R.add) + self.assertEqual(True, R.equals(Just(8), addM(Just(3), Just(5)))) + +if __name__ == '__main__': + unittest.main()