Skip to content

Instantly share code, notes, and snippets.

@pedroteixeira
Created March 9, 2012 19:38
Show Gist options
  • Save pedroteixeira/2008267 to your computer and use it in GitHub Desktop.
Save pedroteixeira/2008267 to your computer and use it in GitHub Desktop.
;; http://pragprog.com/magazines/2012-03/comparing-java-and-scalas-expressiveness
;; first attempt for impl in clojure.. a lot to be improved! :)
(let [lines (map #(clojure.string/split % #" ")
(line-seq (clojure.java.io/reader (clojure.java.io/file "textfile.txt"))))
grouped (group-by #(second %) lines)
parse-long (fn [log] (Long/parseLong (first log)))
diff-times (fn [longs] (for [x (range (count longs)) :let [ts (nth longs x)] ]
(if (= 0 x) ts
(- ts (nth longs (- x 1))))))
times (map (comp diff-times #(map parse-long %)) (vals grouped))]
(print (zipmap (keys grouped) times)))
@guilhermesilveira
Copy link

It is just 1000 times better than the two samples from the article. I am not questioning - and should not - if its funcitonal or non-functional. But the basic patterns of good code are much more present here than there. You dont have useless code, you have split the algorithms into parts and put them together.

Aniche likes to say that people know how to break code into pieces but do not know how to put them together. Thats what you do here... thats what he didnt do there (even breaking it...)

@pedroteixeira
Copy link
Author

for sure, the Java version can be a lot better.
but C#'s LINQ is the best for now https://gist.github.com/2007893 :)

@guilhermesilveira
Copy link

guilhermesilveira commented Mar 9, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment