3 ms·
Only knowing Python and not Ruby, your snippet looks confusing to me. 1. What does the anonymous function return if s.unprocessed is false? 2. If it returns n
by nequo 4y ago
Only knowing Python and not Ruby, your snippet looks confusing to me.
1. What does the anonymous function return if s.unprocessed is false?
2. If it returns nothing, do I get a missing value element in the iterable that results from map?
3. And what does it mean to compact the result of the map call?
The Python snippet raises none of these questions in me. But it could be because I am familiar with Python’s quirks.
- cortesoft 4y agoThe compact gets rid of all the null values returned when unprocessed is false. Interestingly I wasn't exactly sure what the python code would do, which is why I used the compact. In the python code, does 'stuff' end up only having the result of the cleanup function for items that are unprocessed? Or will it have all of the items, and the ones where unprocessed returns false just end up being returned unmodified? My Ruby version returns the processed subset of items that were unprocessed. The python version was not clear to me, since I don't know what happens if s.unprocessed returns false. So basically, it sounds like we were both confused by the same parts of the language we are unfamiliar with... what happens to elements where s.unprocessed is false? It was clear to me that in Ruby it returns a nil, but I have no idea in Python... what happens?
- nequo 4y agoHuh, our mutual confusion is interesting indeed! Thank you for teaching me something about Ruby. What compact does makes sense. Python’s list comprehension combines map and filter. [process(s) for s in stuff if s.unprocessed] is equivalent to map process (filter isUnprocessed stuff) in Haskell.
- baq 4y agoNothing, literally. This is map+filter optimized (or a for+if+append but using dedicated opcodes for efficiency) so you don’t have to do multiple passes and/or create temporary lists with superfluous elements.