Last active
October 7, 2016 07:10
-
-
Save remexre/130b14a717aac2b306fbc7aff4ee39f6 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
;; POSIX-based API. Really long and yuck. | |
(defn capitalize-file (path) | |
; Read the input. | |
(def in-fd (open path)) | |
(def in-str (read in-fd)) | |
(close in-fd) | |
; Process the input. | |
(def out-str (toupper in-str)) | |
; Write the output. | |
(def out-fd (open path 'w)) | |
(write out-fd out-str) | |
(close out-fd)) | |
;; with-x pattern. This is what Python does (if you're doing it right). | |
(defn capitalize-file (path) | |
(def in-str | |
(with-file (f path) | |
(read f))) | |
(def out-str (toupper in-str)) | |
(with-file (f path) | |
(write f out-str))) | |
;; High-level-only approach. Potential problem: this doesn't let you | |
;; generically use non-file IO resources (e.g. stdio and sockets). | |
(defun capitalize-file [path] | |
(->> | |
(read-file path) | |
toupper | |
(write-file path))) | |
;; Something else? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment