Last active
June 6, 2020 01:08
-
-
Save pmonks/4dcd90bcf6d86aad4a5af9282a761c90 to your computer and use it in GitHub Desktop.
Clojure/babashka implementation of wc, inspired by https://github.com/ChrisPenner/wc
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
#!/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))) |
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
#!/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))) |
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
#!/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