(while test & body)
Repeatedly executes body while test expression is true. Presumes some side-effect will cause test to become false/nil. Returns nil
;; a var to be used for its side effects
(def a (atom 10))
;; #'user/a
(while (pos? @a) (do (println @a) (swap! a dec)))
;; 10
;; 9
;; 8
;; 7
;; 6
;; 5
;; 4
;; 3
;; 2
;; 1
;;=> nil
;; calculate the SHA-256 of a file incrementally:
(import
'java.io.File
'javax.xml.bind.DatatypeConverter
'java.security.MessageDigest
'java.security.DigestInputStream)
(require '[clojure.java.io :as io])
(defn md5-file [file]
(let [sha (MessageDigest/getInstance "MD5")]
(with-open [dis (DigestInputStream. (io/input-stream file) sha)]
(while (> (.read dis) -1)))
(DatatypeConverter/printHexBinary (.digest sha))))
(md5-file (File. "/etc/hosts"))
;; "04F186E74288A10E09DFBF8A88D64A1F33C0E698AAA6B75CDB0AC3ABA87D5644"