summaryrefslogtreecommitdiff
path: root/taskflow/utils/iter_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'taskflow/utils/iter_utils.py')
-rw-r--r--taskflow/utils/iter_utils.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/taskflow/utils/iter_utils.py b/taskflow/utils/iter_utils.py
index 68810e8..a96e9cf 100644
--- a/taskflow/utils/iter_utils.py
+++ b/taskflow/utils/iter_utils.py
@@ -16,12 +16,25 @@
# License for the specific language governing permissions and limitations
# under the License.
+import itertools
+
def count(it):
"""Returns how many values in the iterator (depletes the iterator)."""
return sum(1 for _value in it)
+def unique_seen(it, *its):
+ """Yields unique values from iterator(s) (and retains order)."""
+ seen = set()
+ for value in itertools.chain(it, *its):
+ if value in seen:
+ continue
+ else:
+ yield value
+ seen.add(value)
+
+
def find_first_match(it, matcher, not_found_value=None):
"""Searches iterator for first value that matcher callback returns true."""
for value in it: