From 1e55e3f906b76623e1c244ca5bd3f8e1691261e2 Mon Sep 17 00:00:00 2001 From: zyd Date: Tue, 2 Aug 2022 09:46:41 +0900 Subject: [PATCH] Add method ifElse --- README.md | 2 +- ramda/__init__.py | 1 + ramda/ifElse.py | 14 +++++++++ test/test_ifElse.py | 76 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 ramda/ifElse.py create mode 100644 test/test_ifElse.py diff --git a/README.md b/README.md index 60a5c63..b09f400 100644 --- a/README.md +++ b/README.md @@ -291,7 +291,7 @@ R.hasPath(['b'], child) # True - [x] 0.1.2 head - [ ] identical - [x] 0.1.2 identity -- [ ] ifElse +- [x] ifElse - [ ] inc - [ ] includes - [ ] indexBy diff --git a/ramda/__init__.py b/ramda/__init__.py index d462584..4d77291 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -47,6 +47,7 @@ from .hasPath import hasPath from .head import head from .identity import identity +from .ifElse import ifElse from .indexOf import indexOf from .insert import insert from .intersection import intersection diff --git a/ramda/ifElse.py b/ramda/ifElse.py new file mode 100644 index 0000000..a76e9b7 --- /dev/null +++ b/ramda/ifElse.py @@ -0,0 +1,14 @@ +from .curryN import curryN +from .private._curry3 import _curry3 +from .private._inspect import funcArgsLength + + +def inner_ifElse(condition, onTrue, onFalse): + def _ifElse(*args): + if condition(*args): + return onTrue(*args) + return onFalse(*args) + return curryN(max(funcArgsLength(condition), funcArgsLength(onTrue), funcArgsLength(onFalse)), _ifElse) + + +ifElse = _curry3(inner_ifElse) diff --git a/test/test_ifElse.py b/test/test_ifElse.py new file mode 100644 index 0000000..5a8c7c7 --- /dev/null +++ b/test/test_ifElse.py @@ -0,0 +1,76 @@ + +import unittest + +import ramda as R +from ramda.private._inspect import funcArgsLength + +""" +https://github.com/ramda/ramda/blob/master/test/ifElse.js +""" + + +def t(a): return a + 1 +def v(a): return isinstance(a, int) + + +identity = R.identity +def isArray(xs): return isinstance(xs, list) + + +class TestIfElse(unittest.TestCase): + def test_calls_the_truth_case_function_if_the_validator_returns_a_truthy_value(self): + self.assertEqual(11, R.ifElse(v, t, identity)(10)) + + def test_calls_the_false_case_function_if_the_validator_returns_a_falsy_value(self): + def v(a): return isinstance(a, int) + self.assertEqual('hello', R.ifElse(v, t, identity)('hello')) + + def test_calls_the_true_case_on_array_items_and_the_false_case_on_non_array_items(self): + arr = [[1, 2, 3, 4, 5], 10, [0, 1], 15] + arrayToLength = R.map(R.ifElse(isArray, R.length, identity)) + self.assertEqual([5, 10, 2, 15], arrayToLength(arr)) + + def test_passes_the_arguments_to_the_true_case_function(self): + def v(*_): return True + + def onTrue(a, b): + self.assertEqual(a, 123) + self.assertEqual(b, 'abc') + + R.ifElse(v, onTrue, identity)(123, 'abc') + + def test_passes_the_arguments_to_the_false_case_function(self): + def v(*_): return False + + def onFalse(a, b): + self.assertEqual(a, 123) + self.assertEqual(b, 'abc') + + R.ifElse(v, identity, onFalse)(123, 'abc') + + def test_returns_a_function_whose_arity_equals_the_max_arity_of_the_three_arguments_to_ifElse(self): + def a0(): return 0 + def a1(x): return x + def a2(x, y): return x + y + + self.assertEqual(2, funcArgsLength(R.ifElse(a0, a1, a2))) + self.assertEqual(2, funcArgsLength(R.ifElse(a0, a2, a1))) + self.assertEqual(2, funcArgsLength(R.ifElse(a1, a0, a2))) + self.assertEqual(2, funcArgsLength(R.ifElse(a1, a2, a0))) + self.assertEqual(2, funcArgsLength(R.ifElse(a2, a0, a1))) + self.assertEqual(2, funcArgsLength(R.ifElse(a2, a1, a0))) + + def test_returns_a_curried_function(self): + ifIsNumber = R.ifElse(v) + self.assertEqual(16, ifIsNumber(t, identity)(15)) + self.assertEqual('hello', ifIsNumber(t, identity)('hello')) + + fn = R.ifElse(R.gt, R.subtract, R.add) + self.assertEqual(9, fn(2)(7)) + self.assertEqual(9, fn(2, 7)) + self.assertEqual(5, fn(7)(2)) + self.assertEqual(5, fn(7, 2)) + + +if __name__ == '__main__': + unittest.main()