Created
March 1, 2012 16:58
-
-
Save scientific-coder/1951396 to your computer and use it in GitHub Desktop.
Code snippets for Duchess-fr Battle Language 2012-02-29 test it on http://tryclj.com/ !
This file contains 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 fib-seq | |
"lazy seq of Fibonacci numbers" | |
(lazy-cat [0 1] (map + (rest fib-seq) fib-seq))) | |
This file contains 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 fib-seq | |
(map first (iterate (fn[[a b]] [b (+ a b)]) [0 1]))) |
This file contains 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 fizzbuzz | |
"lazy seq of fizzbuzz" | |
(lazy-seq (map #(let [s (str (if (zero? (rem % 3)) "Fizz") | |
(if (zero? (rem % 5)) "Buzz"))] | |
(if (empty? s) % s)) | |
(iterate inc 1)))) |
This file contains 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 fizzbuzzzapp | |
"lazy seq of fizzbuzzzapp more interesting (imo)than fizzbuzz | |
because it starts to pay off to factor some code " | |
(lazy-seq (map #(let [s (apply str (for [[val name] [[3 "Fizz"] | |
[5 "Buzz"] | |
[7 "Zapp"]]] | |
(if (zero? (rem % val)) name)))] | |
(if (empty? s) % s)) | |
(iterate inc 1)))) |
cf https://gist.github.com/2001504#file_menu_reduce2.clj pour une autre
approche. On pourrait aussi faire un mix des deux en mettant un :when dans
le for et en remplaant rsubseq par rseq du coup.
Un conseil que je donne à ceux désireux de casser leurs habitudes est de s'interdire certaines structures trop familières :
- pas de loop pas de recur, pas même de recursion
- pas de mutations
- pas d'indices
- pas de count
- pas de lazy-seq ou de lazy-cat
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Merci pour la suggestion de reduce (cf menu-reduce.clj) : c'est effectivement mieux (car plus simple) à mon avis, dommage que cela soit plus difficile pour moi ☹.