4 ms·
I believe I'm missing something here or my day was too long, but in Clojure this can be squeezed in 13 lines and 435 characters keeping things fairly readable (
by dig1 7y ago
I believe I'm missing something here or my day was too long, but in Clojure this can be squeezed in 13 lines and 435 characters keeping things fairly readable (for Clojure & Lisp developers ;)).
(defn wc [^String file]
(with-open [rdr (clojure.java.io/reader file)]
(apply (partial printf "%d %d %d\n")
(reduce
(fn [[nl nw nb] ^String ln]
(let [words (count (.split ln "[ ]+"))
bytes (alength (.getBytes ln "UTF-8"))]
[(inc nl) (+ nw words) (+ nb bytes)]))
[0 0 0]
(line-seq rdr)))))
(defn -main [& args]
(wc (first args)))
I haven't tested how fast it is, but startup time can be optimized by compiling it with GraalVM.
- deleted 7y ago[deleted]
- tazjin 7y agoNeat! I wonder if the character decoding & regex usage has noticeable performance impact. My Common Lisp version was sped up somewhat by switching from a character stream to a byte stream: https://git.tazj.in/tree/fun/wcl/wc.lisp https://git.tazj.in/tree/fun/wcl/wc.lisp You can try this one via Nix with: nix-build -E '(import (builtins.fetchGit "https://git.tazj.in") {}).fun.wcl'
- patrec 7y agoIt's been a while since I last wrote CL but I think your program can produce any counts between zero and the correct one – you need to use eql instead of eq if you want this to work in standard common lisp.
- tazjin 7y agoAh, you're right of course - fixed. Though that still won't make this portable, as I'm using an SBCL-specific way of accessing argv.
- gmfawcett 7y agoThat's a very impressive Nix configuration. Nicely done!
- gmfawcett 7y agoSince we're golfing, here's a 51-character version in fairly readable J: wc=:[:+/[:(1:,([:#'\S+'&rxmatches),>:@#);._2 freads I'm guessing that a real J wizard could squeeze out a few more characters. :)