4 ms·
It's not Perl all over again. Have you even _read_ those one-liners? Those are neither line-noise, nor fickle one-line imperative solutions. On the contrary,
by batista 14y ago
It's not Perl all over again. Have you even _read_ those one-liners?
Those are neither line-noise, nor fickle one-line imperative solutions.
On the contrary, they are elegant and easy to read --you just have to understand the basics of functional programming, map, reduce, filter and the like (which, in themselves are trivial concepts).
Stuff like those in the article:
(pmap process-line lines)
(dotimes [n 4] (println "Happy Birthday " (if (= n 2) "dear Rich" "to You")))
(reduce max [14 35 -7 46 98])
(def file-text (slurp "data.txt"))
(partition-by #(> % 60) [49 58 76 82 88 90])
(clojure.xml/parse "http://search.twitter.com/search.atom?&q=clojure http://search.twitter.com/search.atom?&q=clojure)
are so easy and clutter-free that what I cannot even begin to comprehend how someone would accuse them of the bad readability that characterises the "Perl one-liner".
- yarrel 14y agoI still hate the square brackets and the slashes. Sorry, hate is such a strong word. I mean that I still find the language's typographic irregularities hopelessly distracting to the point where I won't touch it with a bargepole. They compare to perl one liners more than they do to Lisp one-liners due to their greater syntactic cruft.
- pivo 14y agoI initially disliked the square brakets as well, but I got over it quickly because of the utter happiness I get from writing Clojure code in general. FYI, the square brackets are not an arbitrary deviation from lisp syntax. Square brackets are Clojure's vector syntax, the use of square brackets in function and macro definitions reflects the fact that they are actually vectors. Not sure what you meant with your objection to slashes though.
- Chris_Newton 14y agoTo be fair, comparing something like this: (partition-by #(> % 60) [49 58 76 82 88 90]) to so-called line noise code seems reasonable enough. As with Perl, if you immediately know what all the punctuation means and what the underlying structure of the language is, it’s concise, but if you don’t, it’s quite a mess of random-looking symbols. For comparison, here’s a Haskell version, which in this case has a more intuitive syntax, IMHO: filter (>60) [49, 58, 76, 82, 88, 90] And here’s a version in Scala, with a more explicit function value: List(49, 58, 76, 82, 88, 90).filter(n => n > 60)
- wtetzner 14y ago>(partition-by #(> % 60) [49 58 76 82 88 90]) Yeah, a less noisy way to do it in Clojure would be (partition-by (partial < 60) [49 58 76 82 88 90])
- swuecho 14y agoYou are right. in Perl: grep { $_ > 60 } ( 49, 58, 76, 82, 88, 90)
- etaty 14y agoScala : List(49, 58, 76, 82, 88, 90).filter( _ > 60 )
- Dn_Ab 14y agoI know this is a discussion on syntax but I can't help pointing out that the semantics of paritition-by and filter are very different.
- Chris_Newton 14y agoTrue enough, but I figured filter was widely known and partition-by was perhaps a little esoteric. The syntactic argument would be exactly the same if you used an equivalent of partition-by in the other languages.
- kenko 14y agoReally? There's only two bits of punctuation there, and they don't take much to explain: #(...) is an anonymous function; % is the argument to that function. (I'm not counting > because ... it's just the greater-than sign.) The Haskell version doesn't actually do the same thing. The Haskell returns [76, 82, 88, 90]; the Clojure returns ((49 58) (76 82 88 90))---it groups the input according to the result of the function, it doesn't just filter it.
- Chris_Newton 14y agoIt’s not an ideal example of partition-by anyway, because this particular input has all the values where the predicate returns true together. As you have shown, the result in this case is just to divide the list into the values that match the predicate and those that don’t. This is actually what the “partition” function does in some languages, including Haskell and Scala. However, in general, partition-by in Clojure does more than that and is more like a kind of split algorithm, which this example didn’t demonstrate. In any case, my point here was about syntax, so I used a standard and easily recognisable function in the other languages instead of getting bogged down in trying to reproduce the exact same behaviour. Apologies if this wasn’t clear.