Browse Source

celery.utils.first(predicate, iterable): Like Perl's List::Util::first.

Ask Solem 15 years ago
parent
commit
ea9e1214fe
1 changed files with 8 additions and 0 deletions
  1. 8 0
      celery/utils/__init__.py

+ 8 - 0
celery/utils/__init__.py

@@ -30,6 +30,14 @@ def noop(*args, **kwargs):
     pass
 
 
+def first(predicate, iterable):
+    """Returns the first element in ``iterable`` that ``predicate`` returns a
+    ``True`` value for."""
+    for item in iterable:
+        if predicate(item):
+            return item
+
+
 def chunks(it, n):
     """Split an iterator into chunks with ``n`` elements each.