Created
November 19, 2011 21:48
-
-
Save st/1379405 to your computer and use it in GitHub Desktop.
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 rules {7 "Qix" 5 "Bar" 3 "Foo"}) | |
(defn div [[n s]] | |
(fn [[x accu]] | |
[x (str accu (when (= 0 (rem x n)) s))])) | |
(defn to-ints [x] | |
(map #(- (int %) 48) (seq (str x)))) | |
(defn has [[x accu]] | |
[x (apply str accu (mapcat rules (to-ints x)))]) | |
(def all (cons has (map div (seq rules)))) | |
(defn fbq [n] | |
(let [[n s] ((apply comp all) [n nil])] | |
(if (seq s) s n))) | |
(doseq | |
[i (range 1 101)] | |
(println (fbq i))) |
Je suis arrivé à ça : https://gist.github.com/1507832
Salut Stef, j'étais parti un peu comme toi au début, dans l'idée de faire générique générique, mais quand je me suis heurté au meme problème que toi (à savoir on n'applique pas dans le meme ordre les règles), je suis revenu à de l'application manuelle, et à vrai dire, pour 2 fonctions connues à l'avance, c'est moins overkill ;-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
j'ai l'impression que ça pourrait être plus clair avec un
for [c (str x) :when (= c n)] ....
ou
(re-seq (re-pattern n) (str x))
j'ai pas testé :)