Browse files

First reader forms working

  • Loading branch information...
1 parent f9455bc commit d08eb6af307756d7abf57b3b25f90025423463ce @fogus committed Sep 11, 2012
Showing with 67 additions and 50 deletions.
  1. +1 −1 project.clj
  2. +3 −1 src/data_readers.clj
  3. +16 −3 src/minderbinder/core.clj
  4. +28 −28 src/minderbinder/information.clj
  5. +15 −15 src/minderbinder/time.clj
  6. +4 −2 test/minderbinder/test/core.clj
View
2 project.clj
@@ -1,4 +1,4 @@
-(defproject minderbinder "0.1.0-SNAPSHOT"
+(defproject fogus/minderbinder "0.1.0"
:description "Converting one thing into another thing via Clojure at read time and compile time."
:dependencies [[org.clojure/clojure "1.5.0-alpha3"]]
:plugins [[lein-swank "1.4.4"]
View
4 src/data_readers.clj
@@ -1,2 +1,4 @@
{unit/time minderbinder.time/parse-time-unit
- unit/length minderbinder.time/parse-length-unit}
+ unit/length minderbinder.length/parse-length-unit
+ unit/info minderbinder.information/parse-information-unit}
+
View
19 src/minderbinder/core.clj
@@ -28,12 +28,25 @@
(defmacro defunits-of [quantity base-unit desc & units]
(let [magnitude (gensym)
unit (gensym)
- conversions (build-conversion-map base-unit units)]
+ conversions (build-conversion-map base-unit units)
+ conv-fn (symbol (str "parse-" quantity "-unit"))
+ conv-mac (symbol (str "unit-of-" quantity))]
`(do
- (defmacro ~(symbol (str "unit-of-" quantity)) [~magnitude ~unit]
+ (defmacro ~conv-mac
+ [~magnitude ~unit]
`(* ~~magnitude
~(case ~unit
~@(mapcat
(fn [[u# & r#]]
`[~u# ~(relative-units u# conversions [])])
- conversions)))))))
+ conversions))))
+
+ (defn ~conv-fn
+ [[mag# u#]]
+ (let [conv# ~conversions
+ r# (get conv# u#)]
+ (cond (keyword? r#) (~conv-fn [mag# r#])
+ (vector? r#) (* mag# (~conv-fn r#))
+ :default (* mag# r#))))
+ ~conversions)))
+
View
56 src/minderbinder/information.clj
@@ -1,33 +1,33 @@
(ns minderbinder.information
(use [minderbinder.core :only [defunits-of]]))
-(defunits-of information ::byte
+(defunits-of information :byte
"TODO"
- ::bit 1/8
- ::nibble 1/2
- ::nibble #{::nybble ::nyble}
- ::octet ::byte
- ::kilobyte 1024
- ::kilobyte #{::kibibyte ::KiB ::kB}
- ::megabyte [1024 ::kilobyte]
- ::megabyte #{::mebibyte ::MiB ::MB}
- ::gigabyte [1024 ::megabyte]
- ::gigabyte #{::gibibyte ::GiB ::GB}
- ::terabyte [1024 ::gigabyte]
- ::terabyte #{::tebibyte ::TiB ::TB}
- ::petabyte [1024 ::terabyte]
- ::petabyte #{::pebibyte ::PiB ::PB}
- ::exabyte [1024 ::petabyte]
- ::exabyte #{::exbibyte ::EiB ::EB}
- ::internet [500N ::exabyte] ;; estimate, as of 2009
- ::zettabyte [1024N ::exabyte]
- ::zettabyte #{::zebibyte ::ZiB ::ZB}
- ::yottabyte [1024N ::zettabyte]
- ::yottabyte #{::yobibyte ::YiB ::YB}
+ :bit 1/8
+ :nibble 1/2
+ :nibble #{:nybble :nyble}
+ :octet :byte
+ :kilobyte 1024
+ :kilobyte #{:kibibyte :KiB :kB}
+ :megabyte [1024 :kilobyte]
+ :megabyte #{:mebibyte :MiB :MB}
+ :gigabyte [1024 :megabyte]
+ :gigabyte #{:gibibyte :GiB :GB}
+ :terabyte [1024 :gigabyte]
+ :terabyte #{:tebibyte :TiB :TB}
+ :petabyte [1024 :terabyte]
+ :petabyte #{:pebibyte :PiB :PB}
+ :exabyte [1024 :petabyte]
+ :exabyte #{:exbibyte :EiB :EB}
+ :internet [500N :exabyte] ;; estimate, as of 2009
+ :zettabyte [1024N :exabyte]
+ :zettabyte #{:zebibyte :ZiB :ZB}
+ :yottabyte [1024N :zettabyte]
+ :yottabyte #{:yobibyte :YiB :YB}
- ::kbit [1000 ::bit]
- ::kbit #{::kilobit ::kibit ::kibibit}
- ::Mbit [1000 ::kbit]
- ::Mbit #{::megabit ::Mibit ::mebibit}
- ::Gbit [1000 ::Mbit]
- ::Gbit #{::gigabit ::gibibit ::gibit})
+ :kbit [1000 :bit]
+ :kbit #{:kilobit :kibit :kibibit}
+ :Mbit [1000 :kbit]
+ :Mbit #{:megabit :Mibit :mebibit}
+ :Gbit [1000 :Mbit]
+ :Gbit #{:gigabit :gibibit :gibit})
View
30 src/minderbinder/time.clj
@@ -1,19 +1,19 @@
(ns minderbinder.time
(use [minderbinder.core :only [defunits-of]]))
-(def parse-time-unit identity)
-
-(defunits-of time ::second
+(defunits-of time :millisecond
""
- ::second #{::sec ::s}
- ::minute 60
- ::min ::minute
- ::hr [60 ::min]
- ::hour ::hr
- ::day [24 ::hour]
- ::day #{::d ::da}
- ::week [7 ::day]
- ::week #{::wk ::sennight}
- ::fortnight [14 ::day]
- ::blink [1/100000 ::day]
- ::ce [1/100 ::day])
+ :millisecond #{:ms}
+ :second 1000
+ :second #{:sec :s}
+ :minute [60 :sec]
+ :min :minute
+ :hr [60 :min]
+ :hour :hr
+ :day [24 :hour]
+ :day #{:d :da}
+ :week [7 :day]
+ :week #{:wk :sennight}
+ :fortnight [14 :day]
+ :blink [1/100000 :day]
+ :ce [1/100 :day])
View
6 test/minderbinder/test/core.clj
@@ -1,6 +1,8 @@
(ns minderbinder.test.core
(:use [minderbinder.core] :reload)
+ (:require minderbinder.time)
(:use [clojure.test]))
-(deftest replace-me ;; FIXME: write
- (is false "No tests have been written."))
+(deftest test-time-units
+ (is (= 1000 (minderbinder.time/parse-time-unit [1 :second])))
+ (is (= 1000 #unit/time [1 :second])))

0 comments on commit d08eb6a

Please sign in to comment.