Patchwork [3,of,4,RFC] revset: added __nonzero__ method to lazyset

login
register
mail settings
Submitter Lucas Moscovicz
Date Feb. 20, 2014, 6:36 p.m.
Message ID <44ca956d656db4e57add.1392921392@dev1037.prn2.facebook.com>
Download mbox | patch
Permalink /patch/3728/
State Superseded
Headers show

Comments

Lucas Moscovicz - Feb. 20, 2014, 6:36 p.m.
# HG changeset patch
# User Lucas Moscovicz <lmoscovicz@fb.com>
# Date 1392920138 28800
#      Thu Feb 20 10:15:38 2014 -0800
# Node ID 44ca956d656db4e57add5e815dfd0aaaec57c6b9
# Parent  20cac5a92843ff52c2c4d0af45505cf9a303086b
revset: added __nonzero__ method to lazyset

Now it doesn't have to go through all the set and can return lazily as soon as
it finds one element.
David Soria Parra - Feb. 21, 2014, 3:59 p.m.
On 02/20/2014 10:36 AM, Lucas Moscovicz wrote:
> diff --git a/mercurial/revset.py b/mercurial/revset.py
> --- a/mercurial/revset.py
> +++ b/mercurial/revset.py
> @@ -2143,6 +2143,11 @@
>      def extend(self, x):
>          self.append(x)
>  
> +    def __nonzero__(self):
> +        for r in self:
> +            return True
> +        return False
> +
isn't this equivalent to
def __nonzero__(self):
	return any(self)
which should evaluate lazy and stop after the first element
Matt Mackall - Feb. 21, 2014, 8:38 p.m.
On Fri, 2014-02-21 at 07:59 -0800, David Soria Parra wrote:
> On 02/20/2014 10:36 AM, Lucas Moscovicz wrote:
> > diff --git a/mercurial/revset.py b/mercurial/revset.py
> > --- a/mercurial/revset.py
> > +++ b/mercurial/revset.py
> > @@ -2143,6 +2143,11 @@
> >      def extend(self, x):
> >          self.append(x)
> >  
> > +    def __nonzero__(self):
> > +        for r in self:
> > +            return True
> > +        return False
> > +
> isn't this equivalent to
> def __nonzero__(self):
> 	return any(self)
> which should evaluate lazy and stop after the first element

Python 2.5.

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2143,6 +2143,11 @@ 
     def extend(self, x):
         self.append(x)
 
+    def __nonzero__(self):
+        for r in self:
+            return True
+        return False
+
     def __len__(self):
         # Basic implementation to be changed in future patches.
         l = baseset([r for r in self])