10 Clojure One Liners to Impress Your Friends
I saw an interesting post today aptly titled 10 Scala One Liners to Impress Your Friends and then someone followed up with another blog post titled 10 CoffeeScript One Liners to Impress Your Friends. Those two posts show programming tasks (most of them trivial to solve in modern programming languages) being accomplished in about 1 line of code each.
I thought it’d be quite appropriate if someone ported those examples to my favourite programming language Clojure, so here they are -
Multiple Each Item in a List by 2
(map#(*%2)(range111))
Sum a List of Numbers
(reduce+(range11001))
Verify if Word Exists in a String
I used a regex here, because I believe that’s the right way to do it.
(deftweet"This is an example tweet talking about clojure and emacs.")(defregex(re-pattern(applystr(interpose"|"["clojure""logic""compojure""emacs""macros"]))))(re-seqregextweet); Gives me the actual matches instead of just true/false
As suggested by a commentator, this problem can be solved without using regex by leveraging Clojure’s sets.
(deftweet"This is an example tweet talking about clojure and emacs.")(defis-word?(set["clojure""logic""compojure""emacs""macros"]))(not(nil?(someis-word?(.splittweet" ")))); Returns true/false
Read a File
(deffile-text(slurp"data.txt")); Reads the whole file(deffile-lines(clojure.contrib.io/read-lines"data.txt")); Reads as a sequence of lines
Since Clojure Contrib has been deprecated for future Clojure releases, clojure.contrib.io/read-lines can be rewritten as (line-seq (clojure.java.io/reader (clojure.java.io/file "data.txt"))) in Clojure 1.3 onwards. Thanks to Aaron for pointing it out.
Happy Birthday to You
(doseq[l(map#(str"Happy Birthday "(if(=%2)"dear Rich""to You"))(range4))](printlnl))Alternateversion-(dotimes[n4](println"Happy Birthday "(if(=n2)"dear Rich""to You")))
Filter List of Numbers
(partition-by#(>%60)[495876828890])
Fetch and Parse XML Web Service
(clojure.xml/parse"http://search.twitter.com/search.atom?&q=clojure")
Find Maximum (or Minimum) in a List
(reducemax[1435-74698])(reducemin[1435-74698]);; Now both together((juxt#(reducemax%)#(reducemin%))[1435-74698]); Returns [98 -7]
Parallel Processing
;; Assuming process-line to be a CPU intensive function that operates on a line(pmapprocess-linelines); Note the "p" in front of map
Sieve of Eratosthenes
I don’t I have a sufficiently good (in terms of performance & beauty) one line implementation of SoE. I would recommend checking out Christophe Grand’s treatise on the subject titled Everybody loves the Sieve of Eratosthenes for a great discussion on writing real world prime sieves in Clojure.
Solve FizzBuzz
(map#(cond(zero?(mod%15))"FizzBuzz"(zero?(mod%3))"Fizz"(zero?(mod%5))"Buzz":else%)(range1101))`
Conclusion
Can’t conclude anything from these examples, really. Clojure is an extremely powerful and succinct programming language. Learn it, write some code in it and then decide for yourself.
Have fun!
Fork from: http://freegeek.in/blog/2011/06/10-clojure-one-liners/
通过本帖可以同时学习到 CoffeeScript 与 Scala 10 技..