Skip to content

Instantly share code, notes, and snippets.

@kurogelee
Created March 30, 2014 18:27
Show Gist options
  • Save kurogelee/9877361 to your computer and use it in GitHub Desktop.
Save kurogelee/9877361 to your computer and use it in GitHub Desktop.
Clojureでfuzzy-getとsleep関数を作ってみる ref: http://qiita.com/kurogelee/items/847c7245cb8ef28bf607
user=> (def v {:abc 1 "aiu" 2})
user=> (fuzzy-get v 'ab)
1
user=> (fuzzy-get v :ai)
2
user=> (instance? clojure.lang.Named :a)
true
user=> (instance? clojure.lang.Named 'a)
true
user=> (instance? clojure.lang.Named #'instance?)
false
user=> (instance? clojure.lang.Named (the-ns 'user))
false
(defn name? [x] (or (string? x) (instance? clojure.lang.Named x)))
(defn fuzzy-get [hmap key]
(if (or (not (map? hmap)) (contains? hmap key)) (get hmap key)
(let [f #(if (name? %) (name %) (str %))
ks (sort-by f (keys hmap))]
(some #(when (.startsWith ^String (f %) (f key)) (get hmap %)) ks))))
(def time-unit
(apply hash-map (interleave [:_milliseconds :seconds :minutes :hours :days]
(reductions * [1 1000 60 60 24]))))
(defn ^long milliseconds [^double t unit]
(long (* t (fuzzy-get time-unit unit))))
(defn sleep
([^long ms] (Thread/sleep ms))
([^double t unit] (sleep (milliseconds t unit))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment