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 @@ -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
Expand Down
1 change: 1 addition & 0 deletions ramda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions ramda/insert.py
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions test/test_insert.py
Original file line number Diff line number Diff line change
@@ -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()