comparison clojure/com/aurellem/gb/util.clj @ 288:eec3e69500d9

Made hxc-pokenames and hxc-items return keywords instead of strings by default; added hxc-pokenames-raw and hxc-items-raw for the string version.
author Dylan Holmes <ocsenave@gmail.com>
date Thu, 29 Mar 2012 13:31:31 -0500
parents 69184558fcf3
children 1de6aada1cb7
comparison
equal deleted inserted replaced
287:7918c0dcc0bd 288:eec3e69500d9
27 (defn binary-str [num] 27 (defn binary-str [num]
28 (format "%08d" 28 (format "%08d"
29 (Integer/parseInt 29 (Integer/parseInt
30 (Integer/toBinaryString num) 10))) 30 (Integer/toBinaryString num) 10)))
31 31
32 (defn bit-list
33 "List the bits of n in order of decreasing significance."
34 [n]
35 ((fn this [coll n]
36 (if (zero? n) coll
37 (recur
38 (conj coll (rem n 2))
39 (int (/ n 2)))))
40 [] n))
41
42
43 (defn low-high
44 [low high]
45 (+ low (* 256 high)))
46
47
48 (defn format-name
49 "Convert the string of alphabetic/space characters into a keyword by
50 replacing spaces with hyphens and converting to lowercase."
51 [s]
52 (if (nil? s) nil
53 (keyword (.toLowerCase
54 (apply str
55 (map #(if (= % \space) "-" %) s))))))
56
57
58 ;; used to decode item prices
59
60 (defn decode-bcd
61 "Take a sequence of binary-coded digits (in written order) and return the number they represent."
62 [digits]
63 ((fn self [coll]
64 (if (empty? coll) 0
65 (+ (first coll) (* 100 (self (rest coll))))))
66 (map
67 #(+ (* 10 (int (/ % 16)))
68 (rem % 16))
69 (reverse digits))))
70
71
72
32 73
33 (defn view-register [state name reg-fn] 74 (defn view-register [state name reg-fn]
34 (println (format "%s: %s" name 75 (println (format "%s: %s" name
35 (binary-str (reg-fn state)))) 76 (binary-str (reg-fn state))))
36 state) 77 state)