Created
February 16, 2012 20:14
-
-
Save tsdh/1847513 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; The 10 Scala one-liners tranlated to Clojure. | |
;; http://mkaz.com/solog/scala/10-scala-one-liners-to-impress-your-friends.html | |
;; Oh, there are more of them: | |
;; Python: http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/ | |
;; Ruby: http://programmingzen.com/2011/06/02/10-ruby-one-liners-to-impress-your-friends/ | |
;; CoffeeScript: http://ricardo.cc/2011/06/02/10-CoffeeScript-One-Liners-to-Impress-Your-Friends.html | |
;; Another Clojure version: http://freegeek.in/blog/2011/06/10-clojure-one-liners/ | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 1. Multiple Each Item in a List by 2 | |
(map #(* 2 %) (range 1 11)) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 2. Sum a List of Numbers | |
(reduce + (range 1 11)) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 3. Verify if Exists in a String | |
(let [wl #{"scala" "akka" "play framework" "sbt" "typesafe"} | |
tweet "This is an example tweet talking about scala and sbt."] | |
;; Sets are function of their keys | |
(some wl (clojure.string/split tweet #"\p{Space}"))) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 4. Read in a File | |
(let [my-str (slurp "data.txt") | |
my-seq (clojure.string/split-lines my-str)] | |
;; Or reading lines lazily, so that the file contents don't need to fit in | |
;; memory... | |
(with-open [r (clojure.java.io/reader "data.txt")] | |
(doseq [line (line-seq r)] | |
(do-stuff-with line)))) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 5. Happy Birthday to You! | |
(dotimes [i 3] | |
(println "Happy Birthday" (if (= i 2) "dear NAME" "to You"))) | |
;; Or a bit more like the Scala solution | |
(doseq [s (map #(str "Happy Birthday " (if (= 3 %) "dear NAME" "to You")) | |
[1 2 3])] | |
(println s)) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 6. Filter list of numbers | |
(let [[fail pass] (split-with #(< % 60) [49 58 76 82 88 90])] | |
(println "FAIL:" fail "\nPASS:" pass)) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 7. Fetch and Parse an XML web service | |
(clojure.xml/parse "http://search.twitter.com/search.atom?&q=scala") | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 8. Find minimum (or maximum) in a List | |
(let [v [14 35 -7 46 98]] | |
{:min (reduce min v) | |
:max (reduce max v)}) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 9. Parallel Processing | |
(let [r (pmap #(process-item %) data-list)] | |
(do-stuff-with r)) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;; 10. Sieve of Eratosthenes | |
(defn sieve [n] | |
(let [r (range 2 (inc n))] | |
(reduce (fn [ps x] | |
(if (ps x) | |
(clojure.set/difference ps (range (* x x) n x)) | |
ps)) | |
(set r) | |
r))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment