4 ms·
elts.filter(condition(x)) way more readable.
by eweise 6y ago
elts.filter(condition(x)) way more readable.
- theshrike79 6y agoOnly if you know what a filter is and how the condition is parsed. In more cases than one the condition is a lambda-monstrosity shoved in the filter function, which makes it even harder to parse.
- fomine3 6y agoIn the situation, even for statement also become monster.
- marcus_holmes 6y agoNot if you're not used to it. Literally had this over the past few months working with a junior programmer who had a really hard time understanding stacked map()s and filter()s but understood them easily when I unrolled them into nested for loops.
- eweise 6y agoThe junior programmer would have been much better off learning how a filter function works so they could be productive in almost every other language besides Go.
- marcus_holmes 6y agoThis was Javascript. And yes, I was teaching him how filter functions worked. But the point remains that if you're not used to them, map() and filter(), especially when stacked, are not that readable.
- throwaway894345 6y agoIn my opinion the two are very comparable. If filter() is more readable, it’s not by much, and this is all micro optimization bike shedding. I’ve never had a hard time using the idioms in a given language, and things like map/filter vs for loops were never what pushed me away from or pulled me towards a language. The important things are almost always learning curve, tooling, ecosystem, performance, maintainability, etc and Go does very well in many of those respects. The actual in-language features are generally just gravy. Note that the Python people have map/filter/reduce (and have had them for ages) and no one uses them. It’s either for loops or list comprehensions, and people notoriously struggle with the latter.
- erik_seaberg 6y agoThat's because in Python comprehensions have better ergonomics than lambdas. Scala has comprehensions too for { i <- 1 to 10; if i % 2 == 0 } yield i but sometimes lambdas can be cleaner 1 to 10 filter { _ % 2 == 0 }
- throwaway894345 6y agoThe limitation to lambdas is just the single expression, but the same limitation exists for comprehensions. Comprehensions aren’t particularly readable beyond one loop and a condition or so (I say this begrudgingly as someone who likes to craft complex comprehensions). Beyond that for loops are encouraged, not filter() and friends, which suggests that they are perhaps not as amazing as some in this thread suggest.