Skip to content

Instantly share code, notes, and snippets.

@vinibaggio
Created January 5, 2012 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinibaggio/1566379 to your computer and use it in GitHub Desktop.
Save vinibaggio/1566379 to your computer and use it in GitHub Desktop.
Doubling all the numbers
;; 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)
@vinibaggio
Copy link
Author

1 - I think there's a better construct than the (for);
2 - The (* 2 val) is repeated

@vinibaggio
Copy link
Author

Much better now i guess!

@jeffrydegrande
Copy link

(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 ;)

@jeffrydegrande
Copy link

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

@jeffrydegrande
Copy link

(map #(maybe-double %1 10) [1 2 3 4 5])

@vinibaggio
Copy link
Author

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