4 ms·
Learn You a Haskell for Great Good taught me list comprehension, and I was surprised to see such a pithy shorthand available in Python (+ dict comprehension). T
by jazzyjackson 5y ago
Learn You a Haskell for Great Good taught me list comprehension, and I was surprised to see such a pithy shorthand available in Python (+ dict comprehension). That's a very functional way to transform objects from one shape to another with an in-line expression, no lambda keyword there.
But even the lambda keyword isn't so bad, you can create a dictionary of expressions to call by name, a lot more compact them declaring them the usual way imo: https://github.com/jazzyjackson/py-validate/blob/master/pyvalidate.py#L22 https://github.com/jazzyjackson/py-validate/blob/master/pyva...
To your point, I only recently learned there's a Map function in Python, while in JS I'm .map(x=>y).filter(x=>y).reduce(x=>y)ing left and right.
- frogcoder 5y agoYes! List/Dictionary/Generator comprehension is one big plus for Python, it probably came from the functional world. I use it whenever I can. > But even the lambda keyword isn't so bad, you can create a dictionary of expressions to call by name, a lot more compact them declaring them the usual way imo: https://github.com/jazzyjackson/py-validate/blob/master/pyva https://github.com/jazzyjackson/py-validate/blob/master/pyva... lambda keyword is better than nothing, it definitely can be improved. Just imaging using javascript syntax in your example. > To your point, I only recently learned there's a Map function in Python, while in JS I'm .map(x=>y).filter(x=>y).reduce(x=>y)ing left and right. I think with the introduction of list comprehension Guido saw map function was no longer needed, that was why he wanted it removed. I don't deny it, but using map and filter sometimes are just easier to read. Say [foo(v) for v in a] vs map(foo, a).
- 0x008 5y agoYes but map in Python is riduclously slow when map is combined with a lambda [1] [1]: https://stackoverflow.com/questions/1247486/list-comprehension-vs-map https://stackoverflow.com/questions/1247486/list-comprehensi...
- kzrdude 5y agoIt's a bit hard to find recent numbers in that decade-old thread, but the relevant, more up to date, evidence I see (in this answer) says that map is not much slower than a list comprehension: https://stackoverflow.com/a/57104206 https://stackoverflow.com/a/57104206