On May 9, 2020, at 13:24, Dominik Vilsmeier <[email protected]> wrote:
> 
> 
>> On 09.05.20 22:16, Andrew Barnert wrote:
>>> 
>> There’s an obvious use for the .all, but do you ever have a use for the 
>> elementwise itself? When do you need to iterate all the individual 
>> comparisons? (In numpy, an array of bools has all kinds of uses, starting 
>> with indexing or selecting with it, but I don’t think any of them are doable 
>> here.)
> I probably took too much inspiration from Numpy :-) Also I thought it
> would nicely fit with the builtin `all` and `any`, but you are right,
> there's probably not much use for the elementwise iterator itself. So
> one could use `elementwise` as a namespace for `elementwise.all(chars)
> == string` and `elementwise.any(chars) == string` which automatically
> reduce the elementwise comparisons and the former also performs a length
> check prior to that. This would still leave the option of having
> `elementwise(x) == y` return an iterator without reducing (if desired).

But do you have any use for the .any? Again, it’s useful in NumPy, but would 
any of those uses translate?

If you’re never going to use elementwise.any, and you’re never going to use 
elementwise itself, having elementwise.all rather than just making that the 
callable is just making the useful bit a little harder to access. And it’s 
definitely complicating the implementation, too. If you have a use for the 
other features, that may easily be worth it, but if you don’t, why bother?

I took my lexicompare, stripped out the dependency on other helpers in my 
toolbox (which meant rewriting < in a way that might be a little slower; I 
haven’t tested) and the YAGNI stuff (like trying to be “view-ready” even though 
I never finished my views library), and posted it at 
https://github.com/abarnert/lexicompare (no promises that it’s stdlib-ready 
as-is, of course, but I think it’s at least a useful comparison point here). 
It’s pretty hard to beat this for simplicity:
    @total_ordering
    class _Smallest:
        def __lt__(self, other):
            return True

    @total_ordering
    class lexicompare:
        def __new__(cls, it):
            self = super(lexicompare, cls).__new__(cls)
            self.it = it
            return self
        def __eq__(self, other):
            return all(x==y for x,y in zip_longest(self.it, other, 
fillvalue=object()))
        def __lt__(self, other):
            for x, y in zip_longest(self.it, other, fillvalue=_Smallest()):
                if x < y: return True
                elif x < y: return False
            return False
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/JUG6XZMEGTRYWBUKUVAOXN64FTAJGTX7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to