4 ms·
I'm constantly fighting an internal battle between functional programming and being more pythonic. For example: I once rolled my own lisp style immutable list
by notaddicted 17y ago
I'm constantly fighting an internal battle between functional programming and being more pythonic.
For example: I once rolled my own lisp style immutable list type using nested tuples, i.e. (1,(2,(3,None))) for a problem that they were particularly well suited. (http://en.wikipedia.org/wiki/Knapsack_problem#0-1_knapsack_problem http://en.wikipedia.org/wiki/Knapsack_problem#0-1_knapsack_p...) and got about a 10% speedup. In the end though I threw it out because it was far too ugly.
But I always prefer a def and a map to a for loop.
- rbanffy 17y ago"But I always prefer a def and a map to a for loop." For those I either go with a list comprehension or a generator expression.
- jordanb 17y ago^-- Hear hear. I started getting pretty deep into the functools until I discovered list comprehensions and generators. You get all the concise goodness of the functools while remaining Pythonic. The list comprehension is a really good, intuitive syntax too, I think.