Created
January 5, 2012 17:58
-
-
Save vinibaggio/1566379 to your computer and use it in GitHub Desktop.
Doubling all the numbers
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
;; I have a sequence and I want to double all the numbers up to a limit. The following works, but looks awful! | |
;; (defn double-all [numbers limit] | |
;; (for [val numbers :when (< (* 2 val) limit) ] (* 2 val))) | |
(defn double-all [numbers limit] | |
(filter #(> limit %) (map #(* 2 %) numbers))) | |
(double-all (range 20) 10) |
Much better now i guess!
(defn maybe-double [n, max](let [double %28* 2 n%29] %28cond %28< double max%29 double :else n %29))
perhaps?
(clojure noob here as well ;)
best I can come up with to use it is (map maybe-double [1 2 3 4 5] [10 10 10 10 10]) which isn't very elegant :p
(map #(maybe-double %1 10) [1 2 3 4 5])
Hmm thanks! I'm thinking. I'm using this to implement the Sieve of Erasthotenes... let me check what I can come up with.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1 - I think there's a better construct than the (for);
2 - The (* 2 val) is repeated