Skip to content

Instantly share code, notes, and snippets.

@pmonks
Last active June 6, 2020 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmonks/4dcd90bcf6d86aad4a5af9282a761c90 to your computer and use it in GitHub Desktop.
Save pmonks/4dcd90bcf6d86aad4a5af9282a761c90 to your computer and use it in GitHub Desktop.
Clojure/babashka implementation of wc, inspired by https://github.com/ChrisPenner/wc
#!/usr/bin/env bb
;
; Copyright © 2020 Peter Monks Some Rights Reserved
;
; This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
; To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/ or send a
; letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
;
; Inspired by https://github.com/ChrisPenner/wc - this code diverges from the remaining Haskell examples
(let [file-name (first *command-line-args*)
file (io/file file-name)
char-count (.length file)
lines (line-seq (io/reader file))
[line-count word-count] (loop [f (first lines)
r (rest lines)
lc 0
wc 0]
(if f
(recur (first r)
(rest r)
(inc lc)
(+ wc (count (re-seq #"\S+" f))))
[lc wc ]))]
(println (format " %7d %7d %7d %s"
line-count ; Line count
word-count ; Word count
char-count ; Character count, which is wrong because line-seq strips newlines!
file-name)))
#!/usr/bin/env bb
;
; Copyright © 2020 Peter Monks Some Rights Reserved
;
; This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
; To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/ or send a
; letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
;
; Inspired by https://github.com/ChrisPenner/wc - this code diverges from the remaining Haskell examples
(let [file-name (first *command-line-args*)
lines (line-seq (io/reader file-name))
[line-count word-count char-count] (loop [f (first lines)
r (rest lines)
lc 0
wc 0
cc 0]
(if f
(recur (first r)
(rest r)
(inc lc)
(+ wc (count (re-seq #"\S+" f)))
(+ cc (count f)))
[lc wc cc]))]
(println (format " %7d %7d %7d %s"
line-count ; Line count
word-count ; Word count
char-count ; Character count, which is wrong because line-seq strips newlines!
file-name)))
#!/usr/bin/env bb
;
; Copyright © 2020 Peter Monks Some Rights Reserved
;
; This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
; To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/ or send a
; letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
;
; Inspired by https://github.com/ChrisPenner/wc - this code is similar to the first "stupid" version described there
(let [file-name (first *command-line-args*)
content (slurp file-name)]. ; Note: this assumes the contents of the file fit into the JVM's heap - a BAD assumption to make
(println (format " %7d %7d %7d %s"
(count (re-seq #"\r?\n" content)) ; Line count
(count (re-seq #"\S+" content)) ; Word count
(count content) ; Character count
file-name)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment