4 ms·
5th search result for Closure in Google: http://freegeek.in/blog/2011/06/10-clojure-one-liners/ http://freegeek.in/blog/2011/06/10-clojure-one-liners/ Tell me
by taligent 14y ago
5th search result for Closure in Google:
http://freegeek.in/blog/2011/06/10-clojure-one-liners/ http://freegeek.in/blog/2011/06/10-clojure-one-liners/
Tell me that is not Perl all over again.
- batista 14y agoIt'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.
- espeed 14y agoClojure is on the opposite end of the spectrum from Perl.
- jonathansizz 14y agoYou mean Clojure is unsuitable for writing quick scripts that get things done with the minimum of fuss? The Perl one-liners that actually resemble line-noise get far more done than any of the examples given above. Perl one-liners that do the same as the above examples are actually quite legible.
- draegtun 14y agoThey look fine to me and it also look good written in Perl :) use List::Util qw(sum max min); # 1 map { $_ * 2 } 1..10; # 2 sum 1..1000; # 3 my @wordlist = ("scala", "akka", "play framework", "sbt", "typesafe"); my $tweet = "This is an example tweet talking about scala and sbt."; grep { $tweet =~ m/\b$_\b/ } @wordlist; # 4 my $file_text = do { local $/ = undef; open my $fh, '<', 'data.txt'; <$fh> }; chomp( my @file_lines = do { open my $fh, '<', 'data.txt'; <$fh> } ); # 5 say "Happy Birthday " . ($_ == 3 ? "dear Barry" : "to You") for 1..4; # 6 my ($passed, $failed) = do { my @p; \@p, [map { $_ > 60 ? push(@p, $_) && () : $_ } (49,58,76,82,88,90)] }; # 8 min(14, 35, -7, 46, 98); max(14, 35, -7, 46, 98);
- xyzzyz 14y agomy $file_text = do { local $/ = undef; open my $fh, '<', 'data.txt'; <$fh> }; I have absolutely no idea what this line does. local? undef? '<'?, <$fh>? This is how you read a file in Perl?
- draegtun 14y ago> This is how you read a file in Perl? Nope. I use Path::Class from CPAN... use Path::Class 'file'; my $file_text = file('data.txt')->slurp;
- chromatic 14y agoI have absolutely no idea what this line does. If you don't know Perl, why should you expect to? I didn't know that #(...) represented a lambda in Scala until a moment ago.
- AnswerAndForget 14y ago'Knowing Perl' is a rather hard thing to do when the list of completely ad-hoc implicit variables and hidden context-sensitive surprises is seemingly endless.
- chromatic 14y ago