Skip to content
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 @@ -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
Expand Down
1 change: 1 addition & 0 deletions ramda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions ramda/ifElse.py
Original file line number Diff line number Diff line change
@@ -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)
76 changes: 76 additions & 0 deletions test/test_ifElse.py
Original file line number Diff line number Diff line change
@@ -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()