diff src/utils.clj @ 0:307a81e46071 tip

initial committ
author Robert McIntyre <rlm@mit.edu>
date Tue, 18 Oct 2011 01:17:49 -0700
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/utils.clj	Tue Oct 18 01:17:49 2011 -0700
     1.3 @@ -0,0 +1,52 @@
     1.4 +(ns coderloop.utils
     1.5 +  (:use [clojure.contrib
     1.6 +	 [shell-out :only [sh]]
     1.7 +	 [duck-streams :only [file-str read-lines copy]]])
     1.8 +  (:use [clojure [string :only [trim blank? split]]]))
     1.9 +
    1.10 +
    1.11 +
    1.12 +  
    1.13 +
    1.14 +
    1.15 +(defn read-integer [file]
    1.16 +  (Integer/parseInt
    1.17 +   (trim (slurp (file-str file)))))
    1.18 +  
    1.19 +(defn read-big-integer [file]
    1.20 +  (java.math.BigInteger.
    1.21 +   (trim (slurp (file-str file)))))
    1.22 +
    1.23 +
    1.24 +(defn read-integer-line [#^java.lang.string s]
    1.25 +  (map #(Integer/parseInt %)
    1.26 +       (filter (comp not blank?)
    1.27 +	       (split s #"\W"))))
    1.28 +
    1.29 +
    1.30 +  
    1.31 +(defn read-integers
    1.32 +  "returns a lazy sequence of integers read from the file.
    1.33 +   Integers are delimited by any sort of whitespace"
    1.34 +  [#^java.io.File file]
    1.35 +  (mapcat read-integer-line
    1.36 +	  (filter (comp not blank?)
    1.37 +		  (read-lines (file-str file)))))
    1.38 +  
    1.39 +
    1.40 +(defn trans-print [x]
    1.41 +  (println x )
    1.42 +  x)
    1.43 +
    1.44 +
    1.45 +(defn md5
    1.46 +  "given a string, compute the md5 checksum"
    1.47 +  [s]
    1.48 +  (let [t (java.io.File/createTempFile "zzz" "zzz")]
    1.49 +    (copy s t)
    1.50 +    (.deleteOnExit t)
    1.51 +      (sh "md5sum" (.getCanonicalPath t))))
    1.52 +
    1.53 +
    1.54 +(defn digits [n]
    1.55 +  (map #(Integer/parseInt (str %))  (seq (str n))))