浏览代码

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

Ask Solem 15 年之前
父节点
当前提交
ea9e1214fe
共有 1 个文件被更改,包括 8 次插入0 次删除
  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.