Clojure 10 行令你朋友惊讶的 Clojure 代码

Saito · 2013年01月10日 · 最后由 isaaczeng 回复于 2015年12月26日 · 11402 次阅读

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) (range 1 11))

Sum a List of Numbers

(reduce + (range 1 1001))

Verify if Word Exists in a String

I used a regex here, because I believe that’s the right way to do it.

(def tweet "This is an example tweet talking about clojure and emacs.")
(def regex (re-pattern (apply str (interpose "|" ["clojure" "logic" "compojure" "emacs" "macros"]))))
(re-seq regex tweet) ; 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.

(def tweet "This is an example tweet talking about clojure and emacs.")
(def is-word? (set ["clojure" "logic" "compojure" "emacs" "macros"]))
(not (nil? (some is-word? (.split tweet " ")))) ; Returns true/false

Read a File

(def file-text (slurp "data.txt")) ; Reads the whole file
(def file-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")) (range 4))] (println l))
Alternate version -

(dotimes [n 4] (println "Happy Birthday " (if (= n 2) "dear Rich" "to You")))

Filter List of Numbers

(partition-by #(> % 60) [49 58 76 82 88 90])

Fetch and Parse XML Web Service

(clojure.xml/parse "http://search.twitter.com/search.atom?&q=clojure")

Find Maximum (or Minimum) in a List

(reduce max [14 35 -7 46 98])
(reduce min [14 35 -7 46 98])
;; Now both together
((juxt #(reduce max %) #(reduce min %)) [14 35 -7 46 98]) ; Returns [98 -7]

Parallel Processing

;; Assuming process-line to be a CPU intensive function that operates on a line
(pmap process-line lines) ; 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 %) (range 1 101))`

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 技..

clojure 是不错,感觉一直不温不火,不知道前景怎么样,只能玩一玩。

我觉得上面这些例子没有很好的说明 Clojure 的强大。 我来一例子,定义无穷 Fibonacci 数列:

(def fib-seq (lazy-cat [0 1] (map + fib-seq (rest fib-seq))))
需要 登录 后方可回复, 如果你还没有账号请 注册新账号