filteredset: implement `first` and `last`
authorPierre-Yves David <pierre-yves.david@fb.com>
Tue, 07 Oct 2014 00:18:08 -0700
changeset 22813 5a96df266b2b
parent 22812 fcd12b310148
child 22814 8110405cf8ae
filteredset: implement `first` and `last`
mercurial/revset.py
--- a/mercurial/revset.py	Mon Oct 06 14:42:00 2014 -0700
+++ b/mercurial/revset.py	Tue Oct 07 00:18:08 2014 -0700
@@ -2482,6 +2482,25 @@
     def isdescending(self):
         return self._ascending is not None and not self._ascending
 
+    def first(self):
+        for x in self:
+            return x
+        return None
+
+    def last(self):
+        it = None
+        if self._ascending is not None:
+            if self._ascending:
+                it = self.fastdesc
+            else:
+                it = self.fastasc
+        if it is None:
+            # slowly consume everything. This needs improvement
+            it = lambda: reversed(list(self))
+        for x in it():
+            return x
+        return None
+
 class addset(abstractsmartset):
     """Represent the addition of two sets