From 8e34330b8bbe42962a25543eccc345f6bff8c7d7 Mon Sep 17 00:00:00 2001 From: zyd Date: Sat, 11 Jun 2022 14:53:38 +0900 Subject: [PATCH] add method fromPairs --- README.md | 2 +- ramda/__init__.py | 1 + ramda/fromPairs.py | 13 +++++++++++++ test/test_fromPair.py | 19 +++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 ramda/fromPairs.py create mode 100644 test/test_fromPair.py diff --git a/README.md b/README.md index 23c2400..31ac039 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ R.equals(float('nan'), float('nan')) # True - [x] 0.1.2 flip - [x] 0.1.4 forEach - [ ] forEachObjIndexed -- [ ] fromPairs +- [x] fromPairs - [x] 0.1.2 groupBy - [ ] groupWith - [x] 0.1.2 gt diff --git a/ramda/__init__.py b/ramda/__init__.py index d8f2dee..465a34b 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -32,6 +32,7 @@ from .flatten import flatten from .flip import flip from .forEach import forEach +from .fromPairs import fromPairs from .groupBy import groupBy from .gt import gt from .gte import gte diff --git a/ramda/fromPairs.py b/ramda/fromPairs.py new file mode 100644 index 0000000..ddc6c55 --- /dev/null +++ b/ramda/fromPairs.py @@ -0,0 +1,13 @@ +from .private._curry1 import _curry1 + + +def inner_fromPairs(pairs): + result = {} + idx = 0 + while idx < len(pairs): + result[pairs[idx][0]] = pairs[idx][1] + idx += 1 + return result + + +fromPairs = _curry1(inner_fromPairs) diff --git a/test/test_fromPair.py b/test/test_fromPair.py new file mode 100644 index 0000000..a765a96 --- /dev/null +++ b/test/test_fromPair.py @@ -0,0 +1,19 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/fromPair.js +""" + + +class TestFromPair(unittest.TestCase): + def test_combines_an_array_of_two_element_arrays_into_a_dict(self): + self.assertEqual({'a': 1, 'b': 2, 'c': 3}, R.fromPairs([['a', 1], ['b', 2], ['c', 3]])) + + def test_gives_later_entries_precedence_over_earlier_ones(self): + self.assertEqual({'x': 2}, R.fromPairs([['x', 1], ['x', 2]])) + +if __name__ == '__main__': + unittest.main()