Settings

Theme

Optimizing the any function in python

oliverspohngellert.com

1 points by ospohngellert 7 years ago · 7 comments

Reader

detaro 7 years ago

So you're artificially building something that's gives you some of the benefit of using a generator expression in a list expression, instead of just using a generator expression?

  • ospohngellertOP 7 years ago

    I wouldn't call it artificially building something, but I suppose yes. I also find this code to be much more readable than generator code. Once you understand the function, all you have to do is pass it a function that returns a boolean.

    • detaro 7 years ago

      How is

          any(function_takes_time(i) for i in range(10 ** 3))
      
      harder to read than

          fast_any([(lambda x: (lambda: function_takes_time(x)))(i) for i in range(10 ** 3)])
      • ospohngellertOP 7 years ago

        Yeah, sorry. Looking back into it looks like I was really re-inventing the wheel here. Still a fun exercise for me. Thanks :)

      • ospohngellertOP 7 years ago

        The first one is clearly easier to read than the second. However the first one is slow. I believe fast_any is easier to read than generator code.

        • ospohngellertOP 7 years ago

          Actually, apologies I see now. I always associated generator's with the `yields` keyword, but maybe I have to look into them again. Thanks!

        • detaro 7 years ago

          The first IS generator code. That's a generator expression, note that it does not have the [] of a list expression.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection