Mercurial > lasercutter
view src/clojure/contrib/lazy_seqs.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and2 ;; distribution terms for this software are covered by the Eclipse Public3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can4 ;; be found in the file epl-v10.html at the root of this distribution. By5 ;; using this software in any fashion, you are agreeing to be bound by the6 ;; terms of this license. You must not remove this notice, or any other,7 ;; from this software.8 ;;9 ;; lazy-seqs10 ;;11 ;; == Lazy sequences ==12 ;;13 ;; primes - based on the "naive" implemention described in [1] plus a14 ;; small "wheel" which eliminates multiples of 2, 3, 5, and15 ;; 7 from consideration by incrementing past them. Also inspired16 ;; by code from Christophe Grand in [2].17 ;;18 ;; fibs - all the Fibonacci numbers19 ;;20 ;; powers-of-2 - all the powers of 221 ;;22 ;; == Lazy sequence functions ==23 ;;24 ;; (partition-all, shuffle moved to clojure.core)25 ;; (rand-elt moved to clojure.core/rand-nth)26 ;; (rotations, moved to seq_utils.clj)27 ;; (permutations and combinations moved to combinatorics.clj)28 ;;29 ;; [1] http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf30 ;; [2] http://clj-me.blogspot.com/2008/06/primes.html31 ;;32 ;; scgilardi (gmail)33 ;; Created 07 June 200835 (ns36 ^{:author "Stephen C. Gilardi",37 :doc "38 ==== Lazy sequences ====40 primes - based on the \"naive\" implemention described in [1] plus a41 small \"wheel\" which eliminates multiples of 2, 3, 5, and42 7 from consideration by incrementing past them. Also inspired43 by code from Christophe Grand in [2].45 fibs - all the Fibonacci numbers47 powers-of-2 - all the powers of 249 ==== Lazy sequence functions ====51 (partition-all, shuffle moved to clojure.core)52 (rand-elt moved to clojure.core/rand-nth)53 (rotations, rand-elt moved to seq_utils.clj)54 (permutations and combinations moved to combinatorics.clj)56 [1] http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf57 [2] http://clj-me.blogspot.com/2008/06/primes.html58 "}59 clojure.contrib.lazy-seqs60 (:use clojure.contrib.def))62 ; primes cannot be written efficiently as a function, because63 ; it needs to look back on the whole sequence. contrast with64 ; fibs and powers-of-2 which only need a fixed buffer of 1 or 265 ; previous values.66 (defvar primes67 (concat68 [2 3 5 7]69 (lazy-seq70 (let [primes-from71 (fn primes-from [n [f & r]]72 (if (some #(zero? (rem n %))73 (take-while #(<= (* % %) n) primes))74 (recur (+ n f) r)75 (lazy-seq (cons n (primes-from (+ n f) r)))))76 wheel (cycle [2 4 2 4 6 2 6 4 2 4 6 6 2 6 4 277 6 4 6 8 4 2 4 2 4 8 6 4 6 2 4 678 2 6 6 4 2 4 6 2 6 4 2 4 2 10 2 10])]79 (primes-from 11 wheel))))80 "Lazy sequence of all the prime numbers.")82 (defn fibs83 "Returns a lazy sequence of all the Fibonacci numbers."84 []85 (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1])))87 (defn powers-of-288 "Returns a lazy sequence of all the powers of 2"89 []90 (iterate #(bit-shift-left % 1) 1))