!31 Add prepend recipe

From: @zhang-liang-pengkun 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
This commit is contained in:
openeuler-ci-bot 2023-12-28 09:44:08 +00:00 committed by Gitee
commit d73f010b37
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 85 additions and 1 deletions

View File

@ -0,0 +1,80 @@
From a17e5e4f8d9d9b8e1932bda10a1ab5cf59290deb Mon Sep 17 00:00:00 2001
From: Bo Bayles <bbayles@gmail.com>
Date: Mon, 14 May 2018 21:20:08 -0500
Subject: [PATCH] Add prepend recipe
---
docs/api.rst | 1 +
more_itertools/recipes.py | 15 +++++++++++++++
more_itertools/tests/test_recipes.py | 16 ++++++++++++++++
3 files changed, 32 insertions(+)
diff --git a/docs/api.rst b/docs/api.rst
index 914e5bc..49459f3 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -110,6 +110,7 @@ These tools combine multiple iterables.
.. autofunction:: dotproduct
.. autofunction:: flatten
.. autofunction:: roundrobin
+.. autofunction:: prepend
Summarizing
diff --git a/more_itertools/recipes.py b/more_itertools/recipes.py
index ec5e756..3a7706c 100644
--- a/more_itertools/recipes.py
+++ b/more_itertools/recipes.py
@@ -33,6 +33,7 @@ __all__ = [
'pairwise',
'partition',
'powerset',
+ 'prepend',
'quantify',
'random_combination_with_replacement',
'random_combination',
@@ -548,3 +549,17 @@ def nth_combination(iterable, r, index):
result.append(pool[-1 - n])
return tuple(result)
+
+
+def prepend(value, iterator):
+ """Yield *value*, followed by the elements in *iterator*.
+
+ >>> value = '0'
+ >>> iterator = ['1', '2', '3']
+ >>> list(prepend(value, iterator))
+ ['0', '1', '2', '3']
+
+ To prepend multiple values, see :func:`itertools.chain`.
+
+ """
+ return chain([value], iterator)
diff --git a/more_itertools/tests/test_recipes.py b/more_itertools/tests/test_recipes.py
index f6d7680..81721fd 100644
--- a/more_itertools/tests/test_recipes.py
+++ b/more_itertools/tests/test_recipes.py
@@ -589,3 +589,19 @@ class NthCombinationTests(TestCase):
actual = mi.nth_combination(range(180), 4, 2000000)
expected = (2, 12, 35, 126)
self.assertEqual(actual, expected)
+
+
+class PrependTests(TestCase):
+ def test_basic(self):
+ value = 'a'
+ iterator = iter('bcdefg')
+ actual = list(mi.prepend(value, iterator))
+ expected = list('abcdefg')
+ self.assertEqual(actual, expected)
+
+ def test_multiple(self):
+ value = 'ab'
+ iterator = iter('cdefg')
+ actual = tuple(mi.prepend(value, iterator))
+ expected = ('ab',) + tuple('cdefg')
+ self.assertEqual(actual, expected)
--
2.39.0.windows.2

View File

@ -1,6 +1,6 @@
Name: python-more-itertools
Version: 4.1.0
Release: 8
Release: 9
Summary: An opensource python library wrapping around itertools
License: MIT
URL: https://github.com/erikrose/more-itertools
@ -10,6 +10,7 @@ Patch0000: python37.patch
Patch0001: 0001-fix-for-python-3.7-deprecation-warning-about-importi.patch
Patch0002: 0002-more.py-document-ilen.patch
Patch0003: 0003-Add-map_reduce-function-196.patch
Patch0004: 0004-Add-prepend-recipe.patch
%description
This is a python library for efficient use of itertools utility, which also \
@ -64,6 +65,9 @@ See https://pythonhosted.org/more-itertools/index.html for more information.
%{python3_sitelib}/more_itertools-%{version}-py%{python3_version}.egg-info
%changelog
* Thu Dec 28 2023 zhangliangpengkun<zhangliangpengkun@xfusion.com> - 4.1.0-9
- Add prepend recipe
* Fri Dec 22 2023 zhangliangpengkun<zhangliangpengkun@xfusion.com> - 4.1.0-8
- Add map_reduce function (#196)