PageRenderTime 31ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/clojure/info/kovanovic/camelclojure/util.clj

https://code.google.com/p/camel-clojure/
Clojure | 65 lines | 51 code | 12 blank | 2 comment | 4 complexity | 4d966fa85f457bfab62ddd8146737a63 MD5 | raw file
  1. (ns info.kovanovic.camelclojure.util
  2. (:import (java.util.concurrent TimeUnit)))
  3. ;;; Utilities
  4. (defn parse-options
  5. "create hashmap out of list of keywords and symbols.
  6. from (:a 1 :b 2 3 :c 3 4 5) create {:a (1) :b (2 3) :c (3 4 5)"
  7. ([args]
  8. (parse-options (rest args) () (first args) ()))
  9. ([args-left parsed current-arg current-arg-value]
  10. (if-let [current (first args-left)]
  11. (if (keyword? current)
  12. (recur (rest args-left)
  13. (cons current-arg (cons current-arg-value parsed))
  14. current
  15. ())
  16. (recur (rest args-left)
  17. parsed
  18. current-arg
  19. (cons current current-arg-value)))
  20. (apply sorted-map (cons current-arg (cons current-arg-value parsed))))))
  21. (defn- create-options-applier [keyword-option-map]
  22. (fn [route keyword-args-map]
  23. (let [option-keyword (first keyword-args-map)
  24. ;;add route object in the argument list as first element
  25. option-args (cons route (second keyword-args-map))
  26. option-function (get keyword-option-map option-keyword)]
  27. (if-not (nil? option-function)
  28. (option-function option-args)
  29. route))))
  30. (defn apply-options [route options options-map]
  31. (let [options-applier (create-options-applier options-map)]
  32. (reduce options-applier route (parse-options options))))
  33. (defn third [col]
  34. (second (rest col)))
  35. (defn fourth [col]
  36. (second (rest (rest col))))
  37. (defn arg-count [f]
  38. "count the number of arguments of function"
  39. (let [m (first (.getDeclaredMethods (class f)))
  40. p (.getParameterTypes m)]
  41. (alength p)))
  42. (def unit-enums
  43. #{:microseconds TimeUnit/MICROSECONDS
  44. :milliseconds TimeUnit/MILLISECONDS
  45. :nanoseconds TimeUnit/NANOSECONDS
  46. :seconds TimeUnit/SECONDS})
  47. (def unit-converters
  48. #{:minutes (partial * 60)
  49. :hours (partial * 60 60)
  50. :days (partial * 60 60 24)})
  51. (defn convert-unit-to-seconds [amount timeunit]
  52. (if-let [convert-fn (get unit-converters timeunit)]
  53. (convert-fn amount)))