3 ms·
I wouldn't compare a comprehension to a loop because a loop doesn't typically have an output. If a loop does produce an output it's through side effects. List c
by afandian 8y ago
I wouldn't compare a comprehension to a loop because a loop doesn't typically have an output. If a loop does produce an output it's through side effects. List comprehensions are the same as maps, and generator comprehension even more so, as they can compose functions not values.
And a map can be the same as a projection as found in sql.
- taeric 8y agoLoops in lisp would like a word with you. :) The difference is that many languages have constructs that weren't value producing. It is why some have the ternary operator, but lisp just had if statements. So, to that end, most loops do produce a value. And there are several common ways they do it. In many languages, you have to give the details of how they work. In some, you can only those details and the building if the output is a bit more declarative.
- afandian 8y agoI was coming from the Lisp perspective! Or at least Clojure... Maybe it's a language issue. I would say a 'loop' has a jump and a condition. So that's 'for', 'while' and 'do' in C, JS, Python, Pascal, Java... Lots of languages including Lisp have recursion, maps etc but I wouldn't call that looping. Clojure even has TCO recursion but it only returns one value, not a sequence (unless you accumulate the whole thing)
- taeric 8y agoBut even the LOOP macro has returned values for a long time. It isn't even hard to see how the new stream/collect API if Java is just approaching what LOOP has been doing for decades.
- afandian 8y agoOK agreed there's a construct called loop that returns a single value. My original point was that list comprehensions are closer to sql projections than loops.
- taeric 8y agoApologies, I meant my point more as amusement than winning an argument. I am just now reading Practical Common Lisp, and amusingly one of the first things they do is build a simple query language. Didn't even use loop. So, to your point, the imperative commands of looping can be far removed from what people today call comprehensions. That said, I don't think it is inherent. Just a quirk of history.
- afandian 8y agoNo point scoring inferred. In fact, it's a great illustration of different language and technology communities taking words and ideas and implementing them differently, crucially, with different degrees of specificity.
- dfox 8y agoJava's streams implement the same idea as Common Lisp's streams, which were supposed to be more Common Lisp-like replacement for LOOP (eg. composable and with syntax that does not involve string comparison of symbols). That idea was droped from the final ANSI standard.
- lispm 8y agoSERIES, not streams.
- dragonwriter 8y ago> I wouldn't compare a comprehension to a loop because a loop doesn't typically have an output. Loops in lots of languages have outputs. It's kind of necessary in expression-oriented languages. But it's true that comprehensions are different than Python loops in that way (and are, in fact, equivalent to maps with joins and filters, like an SQL SELECT statement.)
- afandian 8y agoPer my other reply [0] maybe my definition of loop, based on C, was too narrow. I draw a distinction with map, recursion, lazy sequences etc [0] https://news.ycombinator.com/item?id=19591233 https://news.ycombinator.com/item?id=19591233