comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:307a81e46071
1 (ns coderloop.utils
2 (:use [clojure.contrib
3 [shell-out :only [sh]]
4 [duck-streams :only [file-str read-lines copy]]])
5 (:use [clojure [string :only [trim blank? split]]]))
6
7
8
9
10
11
12 (defn read-integer [file]
13 (Integer/parseInt
14 (trim (slurp (file-str file)))))
15
16 (defn read-big-integer [file]
17 (java.math.BigInteger.
18 (trim (slurp (file-str file)))))
19
20
21 (defn read-integer-line [#^java.lang.string s]
22 (map #(Integer/parseInt %)
23 (filter (comp not blank?)
24 (split s #"\W"))))
25
26
27
28 (defn read-integers
29 "returns a lazy sequence of integers read from the file.
30 Integers are delimited by any sort of whitespace"
31 [#^java.io.File file]
32 (mapcat read-integer-line
33 (filter (comp not blank?)
34 (read-lines (file-str file)))))
35
36
37 (defn trans-print [x]
38 (println x )
39 x)
40
41
42 (defn md5
43 "given a string, compute the md5 checksum"
44 [s]
45 (let [t (java.io.File/createTempFile "zzz" "zzz")]
46 (copy s t)
47 (.deleteOnExit t)
48 (sh "md5sum" (.getCanonicalPath t))))
49
50
51 (defn digits [n]
52 (map #(Integer/parseInt (str %)) (seq (str n))))