diff --git a/README.md b/README.md index 7d20ec5..af83239 100644 --- a/README.md +++ b/README.md @@ -215,7 +215,7 @@ R.equals(float('nan'), float('nan')) # True - [x] 0.1.2 indexOf - [ ] init - [ ] innerJoin -- [ ] insert +- [x] insert - [ ] insertAll - [x] 0.1.2 intersection - [ ] intersperse diff --git a/ramda/__init__.py b/ramda/__init__.py index 43938ec..434a378 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -37,6 +37,7 @@ from .head import head from .identity import identity from .indexOf import indexOf +from .insert import insert from .intersection import intersection from .into import into from .invoker import invoker diff --git a/ramda/insert.py b/ramda/insert.py new file mode 100644 index 0000000..b0b2241 --- /dev/null +++ b/ramda/insert.py @@ -0,0 +1,9 @@ +from .private._curry3 import _curry3 + + +def inner_insert(idx, elt, arr): + idx = idx if 0 <= idx < len(arr) else len(arr) + return arr[:idx] + [elt] + arr[idx:] + + +insert = _curry3(inner_insert) diff --git a/test/test_insert.py b/test/test_insert.py new file mode 100644 index 0000000..20eeeb9 --- /dev/null +++ b/test/test_insert.py @@ -0,0 +1,25 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/insert.js +""" + +arr = ['a', 'b', 'c', 'd', 'e'] + + +class TestInsert(unittest.TestCase): + def test_inserts_an_element_into_the_given_list(self): + self.assertEqual(['a', 'b', 'x', 'c', 'd', 'e'], R.insert(2, 'x', arr)) + + def test_inserts_another_list_as_an_element(self): + self.assertEqual(['a', 'b', ['s', 't'], 'c', 'd', 'e'], R.insert(2, ['s', 't'], arr)) + + def test_appends_to_the_end_of_the_list_if_the_index_is_too_large(self): + self.assertEqual(['a', 'b', 'c', 'd', 'e', 'z'], R.insert(8, 'z', arr)) + + +if __name__ == '__main__': + unittest.main()