6 ms·
IMHO these conveniences should just be in the language. List comprehensions, dictionary comprehensions, and short-hand for literals are the kind of syntactic su
by WoodenChair 6y ago
IMHO these conveniences should just be in the language. List comprehensions, dictionary comprehensions, and short-hand for literals are the kind of syntactic sugar that have almost no downsides, save you some RSI, and make code more readable. I'm surprised comprehensions in particular haven't spread to more modern languages.
- meddlepal 6y agoIt doesn't really help that the Go developers are stuck in the early 80's still. I hope this succeeds and forces some changes.
- pansa2 6y ago> I'm surprised comprehensions in particular haven't spread to more modern languages. Comprehensions become unreadable quite quickly - even simple ones have to be read inside-out, and complex (especially nested) ones are much worse. Most modern languages seem to prefer functional-style methods like `map`, `filter` etc, which are more readable in all but the simplest cases.
- kbd 6y ago> which are more readable in all but the simplest cases. Except in practice, the vast majority of comprehensions are simple.
- zmmmmm 6y ago> I'm surprised comprehensions in particular haven't spread to more modern languages. I've come to dislike list comprehensions. Simple ones are ok but they do not handle incremental complexity well. Invariably it means code slowly becomes really unreadable as time goes by because nobody wants to rewrite the list comprehension when one more little tweak stuffed in there will do the job. I much prefer object-functional chaining style ala Scala, Groovy, Ruby etc: some_giant_list .findAll { it.foo > 20 } .groupBy { it.bar } .countBy { it.value.size() > 5 } They scale much better over time as new constructs get inserted as functional additions in the sequential pipeline.
- earthboundkid 6y agoYes, as a dev who started in Python and has done more JS recently, I’ve really come to believe that the chaining of in-line functions is a better approach in the end. Python theoretically has the advantage of working with lists or iterators, but in practice, all you really use are lists.
- sammorrowdrums 6y agoTo be fair many tools use iterator types that are not lists like Django querysets. I feel the nice thing in Python is generator expressions, so for lots of code you can decrease the memory footprint and speed some code up through easy lazy evaluation. I do appreciate the clarity of function chaining, and I think Rust made particularly good decisions around iteration in that way. Function chaining ideally needs good ways to break iteration, skip and things, and JS isn't very feature rich beyond the basic map, reduce, forEach. It feels very incomplete compared to a lot of languages that offer that stuff. Hence the popularity of underscore and then lodash.
- earthboundkid 6y agoIf you’re doing a list comprehension with a Django queryset, you’re risking having a million accidental DB requests
- BiteCode_dev 6y ago> all you really use are lists. The more you get experience in Python, and the less you use specifically lists. In fact, you can spot people getting confortable with the language when they start importing itertools, unless they are data scientists, of course.
- aeturnum 6y agoObviously bad practice can ruin any good thing, but I much prefer list comprehensions for smaller operations and will refactor them as they become unwieldy. If you're in an environment where "nobody really wants to" rewrite stuff that's becoming hard to work with (for whatever reason) it probably doesn't matter which language feature dooms you.
- nsonha 6y agoI'm here to tell you that comprehension in this form is not everyone's thing. Scala's comprehension is more generic and it's actually monad binding. Comprehension as seen in python is only readable for simple computation, so you end up having to use map reduce sometime. That makes 2 ways to do the same thing and the "zen of python" a lie in this instance.
- mrmonkeyman 6y agoJust no.