diff src/clojure/contrib/seq_utils.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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/seq_utils.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,244 @@
     1.4 +;;; seq_utils.clj -- Sequence utilities for Clojure
     1.5 +
     1.6 +;; by Stuart Sierra, http://stuartsierra.com/
     1.7 +;; last updated March 2, 2009
     1.8 +
     1.9 +;; Copyright (c) Stuart Sierra, 2008. All rights reserved.  The use
    1.10 +;; and distribution terms for this software are covered by the Eclipse
    1.11 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
    1.12 +;; which can be found in the file epl-v10.html at the root of this
    1.13 +;; distribution.  By using this software in any fashion, you are
    1.14 +;; agreeing to be bound by the terms of this license.  You must not
    1.15 +;; remove this notice, or any other, from this software.
    1.16 +
    1.17 +
    1.18 +;; Change Log
    1.19 +;;
    1.20 +;; DEPRECATED in 1.2. Some functions promoted to clojure.core and some
    1.21 +;; moved to c.c.seq
    1.22 +;;
    1.23 +;; January 10, 2009 (Stuart Sierra):
    1.24 +;;
    1.25 +;; * BREAKING CHANGE: "includes?" now takes collection as first
    1.26 +;;   argument.  This is more consistent with Clojure collection
    1.27 +;;   functions; see discussion at http://groups.google.com/group/clojure/browse_thread/thread/8b2c8dc96b39ddd7/a8866d34b601ff43
    1.28 +;;
    1.29 +
    1.30 +(ns 
    1.31 +  ^{:author "Stuart Sierra (and others)",
    1.32 +    :deprecated "1.2"
    1.33 +    :doc "Sequence utilities for Clojure"}
    1.34 +  clojure.contrib.seq-utils
    1.35 +  (:import (java.util.concurrent LinkedBlockingQueue TimeUnit)
    1.36 +           (java.lang.ref WeakReference))
    1.37 +  (:refer-clojure :exclude [frequencies shuffle partition-by reductions partition-all group-by flatten]))
    1.38 +
    1.39 +
    1.40 +;; 'flatten' written by Rich Hickey,
    1.41 +;; see http://groups.google.com/group/clojure/msg/385098fabfcaad9b
    1.42 +(defn flatten
    1.43 +  "DEPRECATED. Prefer clojure.core version.
    1.44 +  Takes any nested combination of sequential things (lists, vectors,
    1.45 +  etc.) and returns their contents as a single, flat sequence.
    1.46 +  (flatten nil) returns nil."
    1.47 +  {:deprecated "1.2"}
    1.48 +  [x]
    1.49 +  (filter (complement sequential?)
    1.50 +          (rest (tree-seq sequential? seq x))))
    1.51 +
    1.52 +(defn separate
    1.53 +  "Returns a vector:
    1.54 +   [ (filter f s), (filter (complement f) s) ]"
    1.55 +  [f s]
    1.56 +  [(filter f s) (filter (complement f) s)])
    1.57 +
    1.58 +(defn indexed
    1.59 +  "Returns a lazy sequence of [index, item] pairs, where items come
    1.60 +  from 's' and indexes count up from zero.
    1.61 +
    1.62 +  (indexed '(a b c d))  =>  ([0 a] [1 b] [2 c] [3 d])"
    1.63 +  [s]
    1.64 +  (map vector (iterate inc 0) s))
    1.65 +
    1.66 +;; group-by written by Rich Hickey;
    1.67 +;; see http://paste.lisp.org/display/64190
    1.68 +(defn group-by 
    1.69 +  "DEPRECATED. Prefer clojure.core version.
    1.70 +   Returns a sorted map of the elements of coll keyed by the result of
    1.71 +  f on each element. The value at each key will be a vector of the
    1.72 +  corresponding elements, in the order they appeared in coll."
    1.73 +  {:deprecated "1.2"}
    1.74 +  [f coll]
    1.75 +  (reduce
    1.76 +   (fn [ret x]
    1.77 +     (let [k (f x)]
    1.78 +       (assoc ret k (conj (get ret k []) x))))
    1.79 +   (sorted-map) coll))
    1.80 +
    1.81 +;; partition-by originally written by Rich Hickey;
    1.82 +;; modified by Stuart Sierra
    1.83 +(defn partition-by
    1.84 +  "DEPRECATED. Prefer clojure.core version.
    1.85 +   Applies f to each value in coll, splitting it each time f returns
    1.86 +   a new value.  Returns a lazy seq of lazy seqs."
    1.87 +  {:deprecated "1.2"}
    1.88 +  [f coll]
    1.89 +  (when-let [s (seq coll)]
    1.90 +    (let [fst (first s)
    1.91 +          fv (f fst)
    1.92 +          run (cons fst (take-while #(= fv (f %)) (rest s)))]
    1.93 +      (lazy-seq
    1.94 +       (cons run (partition-by f (drop (count run) s)))))))
    1.95 +
    1.96 +(defn frequencies
    1.97 +  "DEPRECATED. Prefer clojure.core version.
    1.98 +  Returns a map from distinct items in coll to the number of times
    1.99 +  they appear."
   1.100 +  {:deprecated "1.2"}
   1.101 +  [coll]
   1.102 +  (reduce (fn [counts x]
   1.103 +              (assoc counts x (inc (get counts x 0))))
   1.104 +          {} coll))
   1.105 +
   1.106 +;; recursive sequence helpers by Christophe Grand
   1.107 +;; see http://clj-me.blogspot.com/2009/01/recursive-seqs.html
   1.108 +(defmacro rec-seq 
   1.109 + "Similar to lazy-seq but binds the resulting seq to the supplied 
   1.110 +  binding-name, allowing for recursive expressions."
   1.111 + [binding-name & body]
   1.112 +  `(let [s# (atom nil)]
   1.113 +     (reset! s# (lazy-seq (let [~binding-name @s#] ~@body)))))
   1.114 +             
   1.115 +(defmacro rec-cat 
   1.116 + "Similar to lazy-cat but binds the resulting sequence to the supplied 
   1.117 +  binding-name, allowing for recursive expressions."
   1.118 + [binding-name & exprs]
   1.119 +  `(rec-seq ~binding-name (lazy-cat ~@exprs)))
   1.120 +         
   1.121 +     
   1.122 +;; reductions by Chris Houser
   1.123 +;; see http://groups.google.com/group/clojure/browse_thread/thread/3edf6e82617e18e0/58d9e319ad92aa5f?#58d9e319ad92aa5f
   1.124 +(defn reductions
   1.125 +  "DEPRECATED. Prefer clojure.core version.
   1.126 +  Returns a lazy seq of the intermediate values of the reduction (as
   1.127 +  per reduce) of coll by f, starting with init."
   1.128 +  {:deprecated "1.2"}
   1.129 +  ([f coll]
   1.130 +   (if (seq coll)
   1.131 +     (rec-seq self (cons (first coll) (map f self (rest coll))))
   1.132 +     (cons (f) nil)))
   1.133 +  ([f init coll]
   1.134 +   (rec-seq self (cons init (map f self coll)))))
   1.135 +
   1.136 +(defn rotations
   1.137 +  "Returns a lazy seq of all rotations of a seq"
   1.138 +  [x]
   1.139 +  (if (seq x)
   1.140 +    (map
   1.141 +     (fn [n _]
   1.142 +       (lazy-cat (drop n x) (take n x)))
   1.143 +     (iterate inc 0) x)
   1.144 +    (list nil)))
   1.145 +
   1.146 +(defn partition-all
   1.147 +  "DEPRECATED. Prefer clojure.core version.
   1.148 +  Returns a lazy sequence of lists like clojure.core/partition, but may
   1.149 +  include lists with fewer than n items at the end."
   1.150 +  {:deprecated "1.2"}
   1.151 +  ([n coll]
   1.152 +     (partition-all n n coll))
   1.153 +  ([n step coll]
   1.154 +     (lazy-seq
   1.155 +      (when-let [s (seq coll)]
   1.156 +        (cons (take n s) (partition-all n step (drop step s)))))))
   1.157 +  
   1.158 +(defn shuffle
   1.159 +  "DEPRECATED. Prefer clojure.core version.
   1.160 +  Return a random permutation of coll"
   1.161 +  {:deprecated "1.2"}
   1.162 +  [coll]
   1.163 +  (let [l (java.util.ArrayList. coll)]
   1.164 +    (java.util.Collections/shuffle l)
   1.165 +    (seq l)))
   1.166 +
   1.167 +(defn rand-elt
   1.168 +  "DEPRECATED. Prefer clojure.core/rand-nth.
   1.169 +   Return a random element of this seq"
   1.170 +  {:deprecated "1.2"}
   1.171 +  [s]
   1.172 +  (nth s (rand-int (count s))))
   1.173 +
   1.174 +
   1.175 +;; seq-on written by Konrad Hinsen
   1.176 +(defmulti seq-on
   1.177 +  "Returns a seq on the object s. Works like the built-in seq but as
   1.178 +   a multimethod that can have implementations for new classes and types."
   1.179 +  {:arglists '([s])}
   1.180 +  type)
   1.181 +
   1.182 +(defmethod seq-on :default
   1.183 +  [s]
   1.184 +  (seq s))
   1.185 +
   1.186 +
   1.187 +(defn find-first
   1.188 +  "Returns the first item of coll for which (pred item) returns logical true.
   1.189 +  Consumes sequences up to the first match, will consume the entire sequence
   1.190 +  and return nil if no match is found."
   1.191 +  [pred coll]
   1.192 +  (first (filter pred coll)))
   1.193 +
   1.194 +; based on work related to Rich Hickey's seque.
   1.195 +; blame Chouser for anything broken or ugly.
   1.196 +(defn fill-queue
   1.197 +  "filler-func will be called in another thread with a single arg
   1.198 +  'fill'.  filler-func may call fill repeatedly with one arg each
   1.199 +  time which will be pushed onto a queue, blocking if needed until
   1.200 +  this is possible.  fill-queue will return a lazy seq of the values
   1.201 +  filler-func has pushed onto the queue, blocking if needed until each
   1.202 +  next element becomes available.  filler-func's return value is ignored."
   1.203 +  ([filler-func & optseq]
   1.204 +    (let [opts (apply array-map optseq)
   1.205 +          apoll (:alive-poll opts 1)
   1.206 +          q (LinkedBlockingQueue. (:queue-size opts 1))
   1.207 +          NIL (Object.) ;nil sentinel since LBQ doesn't support nils
   1.208 +          weak-target (Object.)
   1.209 +          alive? (WeakReference. weak-target)
   1.210 +          fill (fn fill [x]
   1.211 +                 (if (.get alive?)
   1.212 +                   (if (.offer q (if (nil? x) NIL x) apoll TimeUnit/SECONDS)
   1.213 +                     x
   1.214 +                     (recur x))
   1.215 +                   (throw (Exception. "abandoned"))))
   1.216 +          f (future
   1.217 +              (try
   1.218 +                (filler-func fill)
   1.219 +                (finally
   1.220 +                  (.put q q))) ;q itself is eos sentinel
   1.221 +              nil)] ; set future's value to nil
   1.222 +      ((fn drain []
   1.223 +         weak-target ; force closing over this object
   1.224 +         (lazy-seq
   1.225 +           (let [x (.take q)]
   1.226 +             (if (identical? x q)
   1.227 +               @f  ;will be nil, touch just to propagate errors
   1.228 +               (cons (if (identical? x NIL) nil x)
   1.229 +                     (drain))))))))))
   1.230 +
   1.231 +(defn positions
   1.232 +  "Returns a lazy sequence containing the positions at which pred
   1.233 +   is true for items in coll."
   1.234 +  [pred coll]
   1.235 +  (for [[idx elt] (indexed coll) :when (pred elt)] idx))
   1.236 +
   1.237 +(defn includes?
   1.238 +  "Returns true if coll contains something equal (with =) to x,
   1.239 +  in linear time. Deprecated. Prefer 'contains?' for key testing,
   1.240 +  or 'some' for ad hoc linear searches."
   1.241 +  {:deprecated "1.2"}
   1.242 +  [coll x]
   1.243 +  (boolean (some (fn [y] (= y x)) coll)))
   1.244 +
   1.245 +
   1.246 +
   1.247 +