Created
March 30, 2014 18:27
-
-
Save kurogelee/9877361 to your computer and use it in GitHub Desktop.
Clojureでfuzzy-getとsleep関数を作ってみる ref: http://qiita.com/kurogelee/items/847c7245cb8ef28bf607
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user=> (def v {:abc 1 "aiu" 2}) | |
user=> (fuzzy-get v 'ab) | |
1 | |
user=> (fuzzy-get v :ai) | |
2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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