Skip to content

Instantly share code, notes, and snippets.

@remexre
Last active October 7, 2016 07:10
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 remexre/130b14a717aac2b306fbc7aff4ee39f6 to your computer and use it in GitHub Desktop.
Save remexre/130b14a717aac2b306fbc7aff4ee39f6 to your computer and use it in GitHub Desktop.
;; 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