Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method assoc #175

Merged
merged 1 commit into from
Aug 12, 2022
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ R.add(date(1,2,3), date(1,2,3)) # float('nan)
- [ ] applySpec
- [ ] applyTo
- [ ] ascend
- [ ] assoc
- [x] assoc

Currently, we only support list and dict type.

- [x] assocPath

Currently, we only support list and dict type.
Expand Down
1 change: 1 addition & 0 deletions ramda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .ap import ap
from .append import append
from .apply import apply
from .assoc import assoc
from .assocPath import assocPath
from .binary import binary
from .chain import chain
Expand Down
4 changes: 4 additions & 0 deletions ramda/assoc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .assocPath import assocPath
from .private._curry3 import _curry3

assoc = _curry3(lambda prop, val, obj: assocPath([prop], val, obj))
54 changes: 54 additions & 0 deletions test/test_assoc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

import unittest

import ramda as R

"""
https://github.com/ramda/ramda/blob/master/test/assoc.js
"""


class TestAssoc(unittest.TestCase):
def test_makes_a_shallow_clone_of_an_object_overriding_only_the_specific_property(self):
obj1 = {'a': 1, 'b': {'c': 2, 'd': 3}, 'e': 4, 'f': 5}
obj2 = R.assoc('e', {'x': 42}, obj1)
self.assertEqual({'a': 1, 'b': {'c': 2, 'd': 3}, 'e': {'x': 42}, 'f': 5}, obj2)

self.assertEqual(obj1['a'], obj2['a'])
self.assertEqual(obj1['b'], obj2['b'])
self.assertEqual(obj1['f'], obj2['f'])

def test_is_the_equivalent_of_clone_and_set_if_the_property_is_not_on_the_original(self):
obj1 = {'a': 1, 'b': {'c': 2, 'd': 3}, 'e': 4, 'f': 5}
obj2 = R.assoc('z', {'x': 42}, obj1)
self.assertEqual({'a': 1, 'b': {'c': 2, 'd': 3}, 'e': 4, 'f': 5, 'z': {'x': 42}}, obj2)

self.assertEqual(obj1['a'], obj2['a'])
self.assertEqual(obj1['b'], obj2['b'])
self.assertEqual(obj1['f'], obj2['f'])

def test_makes_a_shallow_clone_of_an_array_overriding_only_the_specific_index(self):
newValue = [4, 2]
arr1 = [1, [2, 3], 4, 5]
arr2 = R.assoc(2, newValue, arr1)
self.assertEqual([1, [2, 3], [4, 2], 5], arr2)

self.assertEqual(arr1[0], arr2[0])
self.assertEqual(arr1[1], arr2[1])
self.assertEqual(newValue, arr2[2])
self.assertEqual(arr1[3], arr2[3])

def test_is_the_equivalent_of_clone_and_set_if_the_index_is_not_on_the_original(self):
newValue = [4, 2]
arr1 = [1, [2, 3], 4]
arr2 = R.assoc(5, newValue, arr1)
self.assertEqual([1, [2, 3], 4, None, None, [4, 2]], arr2)

self.assertEqual(arr1[0], arr2[0])
self.assertEqual(arr1[1], arr2[1])
self.assertEqual(arr1[2], arr2[2])
self.assertEqual(newValue, arr2[5])


if __name__ == '__main__':
unittest.main()