5 ms·
Not really a fan of the lambda syntax in Python. Comparison: JavaScript: let arr = [1, 2, 3] let sumOfSquares = arr.map(n => n * n).reduce((a, b) => a + b
by saadq 10y ago
Not really a fan of the lambda syntax in Python. Comparison:
JavaScript:
let arr = [1, 2, 3]
let sumOfSquares = arr.map(n => n * n).reduce((a, b) => a + b) // 14
Python:
arr = [1, 2, 3]
sum_of_squares = reduce(lambda a, b: a + b, map(lambda n: n * n, arr)) # 14
- brianwawok 10y agoIs there a more Pythonic way to do it? Lambdas are cool but usually not the first place you go in Python. I would think something like (my best guess, not a Python pro)... sum_of_squares = sum([x*x for x in arr]) Which I think is easier to read than either example post above. Of course you will point out that this is less powerful than full map and reduce.. but meh... pros and cons to both styles
- FreeFull 10y agoI would write it like this, to avoid constructing the immediate list: sum_of_squares = sum(x*x for x in arr) This makes use of https://www.python.org/dev/peps/pep-0289/ https://www.python.org/dev/peps/pep-0289/
- brianwawok 10y agoThanks for the link, this is good to know.
- akavi 10y agoWhat's the advantage of list comprehension over lambdas (assuming the lambda syntax is decently lightweight)? I feel like I come down hard on the side of lambdas, but I've never really spent enough time in a language with list comprehension, so there's a good chance I'm missing something.
- brianwawok 10y agoI think it's just less to type really, and it's considered the more standard way to do it.
- cwilkes 10y agoI find it a lot easier to read, part of which is that I'm used to the Scala way of sequence dot map function. When I see the python one I can't remember if the function comes first or the array.
- warbiscuit 10y agoI'm not positive, but I think it saves the need to create a new execution frame for each lambda call, since the whole loop executes in single frame used by the comprehension. In theory I suppose the VM could have a map() implementation which opportunistically extracts the code from a lambda and inlines them when possible; but doubt CPython does that. OTOH, I'd be surprised if PyPy doesn't do something like that.
- fovc 10y agoSince Python 3, both generators and lists create a new stack frame. [1] (2nd to last paragraph) [1] http://python-history.blogspot.com/2010/06/from-list-comprehensions-to-generator.html http://python-history.blogspot.com/2010/06/from-list-compreh...
- warbiscuit 10y agoI'm not meaning when the comprehension is invoked, but during each iteration of the loop within the comprehension. When doing something like `map(lambda x: 2+x, range(100))`, there will be 101 frames created: the outer frame, and 100 for each invocation of the lambda. Whereas `[2+x for x in range(100)]` will only create 2: one for the outer frame, and one for the comprehension.
- quietwhisper 10y agohow can you come down hard on the side of one when you've never experienced the other? I'm from a non-list-comprehension background too, but recently started working a lot in a large python codebase, and have found the dict/list comprehensions to be beautiful. I'm a huge fan. It's a shame lambda syntax is not the best and it's generally crippled, but comprehensions are a great 80/20 compromise for handling most cases very cleanly.
- S4M 10y agoThat example works only because the function sum is already defined in Python. If you wanted to do something less common than summing up elements you would have to either use reduce or implement a for loop.
- brianwawok 10y agoWell, or write a static method somewhere that you call. Sum is used a lot, so handy it is written somewhere (vs having to do a lambda x + x thing.
- nawitus 10y agoThat seems like an argument against lambda functions in general - why use lambdas when you can define a static function for every case? Well, the answer in my opinion is because it makes code more readable if you can define a simple lambda function instead of having to name every single function in the code base.
- brianwawok 10y agoWell if you are going to reuse the function, name it. If it is a 1 time thing, use a lambda.
- quietwhisper 10y agoSounds great in theory. Problem is if you need a lambda that isn't a single expression, you then have to name it. Welcome to the conversation.
- zardeh 10y agoTrue, but I've used python a lot, and I've used reduce maybe...twice? (well, twice that I can find on my github at least)
- S4M 10y agoI just counted the number of reduce I used in my current python project (6k lines). reduce comes up 32 times. And by comparison, map is used 159 times and filter 125 times - for some reason I tend to use list comprehensions less than I should.
- numlocked 10y agoWorth noting that map() can be parallelized whereas a list comprehension can't necessarily (since it is an explicit loop). The multiprocessing module allows trivial map parallelization, but can't work on list comprehensions. It's more than just stylistic.
- riffraff 10y agoI don't think this is the case, list comprehensions can be expressed as syntax sugar over list functions, it's how they work in Scala for example http://docs.scala-lang.org/tutorials/FAQ/yield.html http://docs.scala-lang.org/tutorials/FAQ/yield.html
- brianwawok 10y agoSo I have coded everything from dumb web servers (tm), to high performance trading engines (tm). I have toyed with doing the list in parallel thing... and used it in a toy GUI tool or two I wrote... but never really found it that useful in the real world. If you actually want high performance, doing a parallel map is not going to be fast enough. If you are a dumb web server, it's a waste of overhead 99% of the time. But hey, if you want to use map when you actually need to do a parallel map, cool. But seems very very uncommon. ~ 1 in 10,000 maps I write.
- fovc 10y agomap() can only be parallelized if the function has no side effects. If there are no side effects, list comprehensions can be parallelized just as well
- deleted 10y ago[deleted]
- hcrisp 10y agoFor simple mathematical operations you can import them as functions: from operator import mul, add arr = [1, 2, 3] sum_of_squares = reduce(add, map(mul, arr, arr))
- flaie 10y agoIt's even more concise in Clojure: (defn sum-of-squares [a] (reduce + (map #(* % %) a))) (sum-of-squares [1, 2, 3]) ; => 14