Skip to content

Instantly share code, notes, and snippets.

@cgrand
Forked from laurentpetit/gist:1233437
Created September 23, 2011 06:05
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 cgrand/1236838 to your computer and use it in GitHub Desktop.
Save cgrand/1236838 to your computer and use it in GitHub Desktop.
coding-dojo-20110921
; juste pour l'art
(defn primes []
(keep :prime
(iterate (fn [{:keys [known candidate]}]
(if (some #(zero? (rem candidate %)) known)
{:known known :candidate (inc candidate)}
{:known (conj known candidate) :candidate (inc candidate) :prime candidate}))
{:known [] :candidate 2})))
(def primes
(lazy-seq (cons 2 (remove (fn [candidate] (some #(zero? (rem candidate %)) (take-while #(< % candidate) primes)))
(iterate inc 3)))))
@cgrand
Copy link
Author

cgrand commented Sep 23, 2011

L11-13 l'exemple même de code trop malin pour son propre bien : utilise la mutation de manière cachée, dépend du caractère non-chunké de (iterate inc 3).
Clojure n'est pas Haskell, la paresse ne sert pas les mêmes objectifs. En Haskell c'est à la base du contrôle du flot d'exécution, en Clojure c'est juste le "media" du traitement, comme les pipes unix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment