Skip to content

Commit

Permalink
update contrib to remove seq fns promoted to clojure.core
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthalloway committed Apr 28, 2010
1 parent a1c66df commit 78ee9b3
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 258 deletions.
3 changes: 1 addition & 2 deletions src/main/clojure/clojure/contrib/command_line.clj
Expand Up @@ -12,11 +12,10 @@
#^{:author "Chris Houser",
:doc "Process command-line arguments according to a given cmdspec"}
clojure.contrib.command-line
(:require (clojure.contrib [seq :as su]))
(:use (clojure.contrib [string :only (join)])))

(defn make-map [args cmdspec]
(let [{spec true [rest-sym] false} (su/group-by vector? cmdspec)
(let [{spec true [rest-sym] false} (group-by vector? cmdspec)
rest-str (str rest-sym)
key-data (into {} (for [[syms [_ default]] (map #(split-with symbol? %)
(conj spec '[help? h?]))
Expand Down
3 changes: 1 addition & 2 deletions src/main/clojure/clojure/contrib/datalog/literals.clj
Expand Up @@ -18,8 +18,7 @@
(:use clojure.contrib.datalog.util)
(:use clojure.contrib.datalog.database)
(:use [clojure.set :only (intersection)])
(:use [clojure.contrib.set :only (subset?)])
(:use [clojure.contrib.seq :only (flatten)]))
(:use [clojure.contrib.set :only (subset?)]))


;;; Type Definitions
Expand Down
8 changes: 6 additions & 2 deletions src/main/clojure/clojure/contrib/lazy_seqs.clj
Expand Up @@ -21,7 +21,9 @@
;;
;; == Lazy sequence functions ==
;;
;; (rotations, partition-all, shuffle, rand-elt moved to seq_utils.clj)
;; (partition-all, shuffle moved to clojure.core)
;; (rand-elt moved to clojure.core/rand-nth)
;; (rotations, moved to seq_utils.clj)
;; (permutations and combinations moved to combinatorics.clj)
;;
;; [1] http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
Expand All @@ -46,7 +48,9 @@
==== Lazy sequence functions ====
(rotations, partition-all, shuffle, rand-elt moved to seq_utils.clj)
(partition-all, shuffle moved to clojure.core)
(rand-elt moved to clojure.core/rand-nth)
(rotations, rand-elt moved to seq_utils.clj)
(permutations and combinations moved to combinatorics.clj)
[1] http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
Expand Down
10 changes: 5 additions & 5 deletions src/main/clojure/clojure/contrib/monads.clj
Expand Up @@ -268,13 +268,13 @@
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn- flatten
(defn- flatten*
"Like #(apply concat %), but fully lazy: it evaluates each sublist
only when it is needed."
[ss]
(lazy-seq
(when-let [s (seq ss)]
(concat (first s) (flatten (rest s))))))
(concat (first s) (flatten* (rest s))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
Expand Down Expand Up @@ -313,10 +313,10 @@
[m-result (fn m-result-sequence [v]
(list v))
m-bind (fn m-bind-sequence [mv f]
(flatten (map f mv)))
(flatten* (map f mv)))
m-zero (list)
m-plus (fn m-plus-sequence [& mvs]
(flatten mvs))
(flatten* mvs))
])

; Set monad
Expand Down Expand Up @@ -543,7 +543,7 @@
(fn m-bind-sequence-t [mv f]
(m-bind mv
(fn [xs]
(m-fmap flatten
(m-fmap flatten*
(m-map f xs))))))
m-zero (with-monad m (m-result (list)))
m-plus (with-monad m
Expand Down
4 changes: 2 additions & 2 deletions src/main/clojure/clojure/contrib/pprint/cl_format.clj
Expand Up @@ -249,7 +249,7 @@ for improved performance"
(clojure.core/format format-str val)
(base-str base val))))

(defn- group-by [unit lis]
(defn- group-by* [unit lis]
(reverse
(first
(consume (fn [x] [(seq (reverse (take unit x))) (seq (drop unit x))]) (reverse lis)))))
Expand All @@ -261,7 +261,7 @@ for improved performance"
pos-arg (if neg (- arg) arg)
raw-str (opt-base-str base pos-arg)
group-str (if (:colon params)
(let [groups (map #(apply str %) (group-by (:commainterval params) raw-str))
(let [groups (map #(apply str %) (group-by* (:commainterval params) raw-str))
commas (repeat (count groups) (:commachar params))]
(apply str (next (interleave commas groups))))
raw-str)
Expand Down
99 changes: 11 additions & 88 deletions src/main/clojure/clojure/contrib/seq.clj
Expand Up @@ -14,13 +14,23 @@

;; Change Log
;;
;; April 28, 2010 (Stuart Halloway):
;;
;; * BREAKING CHANGE:
;; moved to clojure.core: flatten, partition-all, frequencies,
;; reductions, shuffle, partition-by
;; moved with semantic changes:
;; group-by now returns an *unsorted* map
;; moved with name changes:
;; rand-elt => clojure.core/rand-nth
;; includes? => clojure.core/seq-contains?
;;
;; January 10, 2009 (Stuart Sierra):
;;
;; * BREAKING CHANGE: "includes?" now takes collection as first
;; argument. This is more consistent with Clojure collection
;; functions; see discussion at http://groups.google.com/group/clojure/browse_thread/thread/8b2c8dc96b39ddd7/a8866d34b601ff43


(ns
#^{:author "Stuart Sierra (and others)",
:doc "Sequence utilities for Clojure"}
Expand All @@ -29,29 +39,12 @@
(java.lang.ref WeakReference)))


;; 'flatten' written by Rich Hickey,
;; see http://groups.google.com/group/clojure/msg/385098fabfcaad9b
(defn flatten
"Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat sequence.
(flatten nil) returns nil."
[x]
(filter (complement sequential?)
(rest (tree-seq sequential? seq x))))

(defn separate
"Returns a vector:
[ (filter f s), (filter (complement f) s) ]"
[f s]
[(filter f s) (filter (complement f) s)])

(defn includes?
"Returns true if coll contains something equal (with =) to x,
in linear time."
[coll x]
(if (some (fn [y] (= y x)) coll)
true false))

(defn indexed
"Returns a lazy sequence of [index, item] pairs, where items come
from 's' and indexes count up from zero.
Expand All @@ -60,40 +53,6 @@
[s]
(map vector (iterate inc 0) s))

;; group-by written by Rich Hickey;
;; see http://paste.lisp.org/display/64190
(defn group-by
"Returns a sorted map of the elements of coll keyed by the result of
f on each element. The value at each key will be a vector of the
corresponding elements, in the order they appeared in coll."
[f coll]
(reduce
(fn [ret x]
(let [k (f x)]
(assoc ret k (conj (get ret k []) x))))
(sorted-map) coll))

;; partition-by originally written by Rich Hickey;
;; modified by Stuart Sierra
(defn partition-by
"Applies f to each value in coll, splitting it each time f returns
a new value. Returns a lazy seq of lazy seqs."
[f coll]
(when-let [s (seq coll)]
(let [fst (first s)
fv (f fst)
run (cons fst (take-while #(= fv (f %)) (rest s)))]
(lazy-seq
(cons run (partition-by f (drop (count run) s)))))))

(defn frequencies
"Returns a map from distinct items in coll to the number of times
they appear."
[coll]
(reduce (fn [counts x]
(assoc counts x (inc (get counts x 0))))
{} coll))

;; recursive sequence helpers by Christophe Grand
;; see http://clj-me.blogspot.com/2009/01/recursive-seqs.html
(defmacro rec-seq
Expand All @@ -109,19 +68,6 @@
[binding-name & exprs]
`(rec-seq ~binding-name (lazy-cat ~@exprs)))


;; reductions by Chris Houser
;; see http://groups.google.com/group/clojure/browse_thread/thread/3edf6e82617e18e0/58d9e319ad92aa5f?#58d9e319ad92aa5f
(defn reductions
"Returns a lazy seq of the intermediate values of the reduction (as
per reduce) of coll by f, starting with init."
([f coll]
(if (seq coll)
(rec-seq self (cons (first coll) (map f self (rest coll))))
(cons (f) nil)))
([f init coll]
(rec-seq self (cons init (map f self coll)))))

(defn rotations
"Returns a lazy seq of all rotations of a seq"
[x]
Expand All @@ -132,29 +78,6 @@
(iterate inc 0) x)
(list nil)))

(defn partition-all
"Returns a lazy sequence of lists like clojure.core/partition, but may
include lists with fewer than n items at the end."
([n coll]
(partition-all n n coll))
([n step coll]
(lazy-seq
(when-let [s (seq coll)]
(cons (take n s) (partition-all n step (drop step s)))))))

(defn shuffle
"Return a random permutation of coll"
[coll]
(let [l (java.util.ArrayList. coll)]
(java.util.Collections/shuffle l)
(seq l)))

(defn rand-elt
"Return a random element of this seq"
[s]
(nth s (rand-int (count s))))


;; seq-on written by Konrad Hinsen
(defmulti seq-on
"Returns a seq on the object s. Works like the built-in seq but as
Expand Down
98 changes: 11 additions & 87 deletions src/main/clojure/clojure/contrib/seq_utils.clj
Expand Up @@ -14,6 +14,17 @@

;; Change Log
;;
;; April 28, 2010 (Stuart Halloway):
;;
;; * BREAKING CHANGE:
;; moved to clojure.core: flatten, partition-all, frequencies,
;; reductions, shuffle, partition-by
;; moved with semantic changes:
;; group-by now returns an *unsorted* map
;; moved with name changes:
;; rand-elt => clojure.core/rand-nth
;; includes? => clojure.core/seq-contains?
;;
;; January 10, 2009 (Stuart Sierra):
;;
;; * BREAKING CHANGE: "includes?" now takes collection as first
Expand All @@ -29,29 +40,12 @@
(java.lang.ref WeakReference)))


;; 'flatten' written by Rich Hickey,
;; see http://groups.google.com/group/clojure/msg/385098fabfcaad9b
(defn flatten
"Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat sequence.
(flatten nil) returns nil."
[x]
(filter (complement sequential?)
(rest (tree-seq sequential? seq x))))

(defn separate
"Returns a vector:
[ (filter f s), (filter (complement f) s) ]"
[f s]
[(filter f s) (filter (complement f) s)])

(defn includes?
"Returns true if coll contains something equal (with =) to x,
in linear time."
[coll x]
(if (some (fn [y] (= y x)) coll)
true false))

(defn indexed
"Returns a lazy sequence of [index, item] pairs, where items come
from 's' and indexes count up from zero.
Expand All @@ -60,40 +54,6 @@
[s]
(map vector (iterate inc 0) s))

;; group-by written by Rich Hickey;
;; see http://paste.lisp.org/display/64190
(defn group-by
"Returns a sorted map of the elements of coll keyed by the result of
f on each element. The value at each key will be a vector of the
corresponding elements, in the order they appeared in coll."
[f coll]
(reduce
(fn [ret x]
(let [k (f x)]
(assoc ret k (conj (get ret k []) x))))
(sorted-map) coll))

;; partition-by originally written by Rich Hickey;
;; modified by Stuart Sierra
(defn partition-by
"Applies f to each value in coll, splitting it each time f returns
a new value. Returns a lazy seq of lazy seqs."
[f coll]
(when-let [s (seq coll)]
(let [fst (first s)
fv (f fst)
run (cons fst (take-while #(= fv (f %)) (rest s)))]
(lazy-seq
(cons run (partition-by f (drop (count run) s)))))))

(defn frequencies
"Returns a map from distinct items in coll to the number of times
they appear."
[coll]
(reduce (fn [counts x]
(assoc counts x (inc (get counts x 0))))
{} coll))

;; recursive sequence helpers by Christophe Grand
;; see http://clj-me.blogspot.com/2009/01/recursive-seqs.html
(defmacro rec-seq
Expand All @@ -109,19 +69,6 @@
[binding-name & exprs]
`(rec-seq ~binding-name (lazy-cat ~@exprs)))


;; reductions by Chris Houser
;; see http://groups.google.com/group/clojure/browse_thread/thread/3edf6e82617e18e0/58d9e319ad92aa5f?#58d9e319ad92aa5f
(defn reductions
"Returns a lazy seq of the intermediate values of the reduction (as
per reduce) of coll by f, starting with init."
([f coll]
(if (seq coll)
(rec-seq self (cons (first coll) (map f self (rest coll))))
(cons (f) nil)))
([f init coll]
(rec-seq self (cons init (map f self coll)))))

(defn rotations
"Returns a lazy seq of all rotations of a seq"
[x]
Expand All @@ -132,29 +79,6 @@
(iterate inc 0) x)
(list nil)))

(defn partition-all
"Returns a lazy sequence of lists like clojure.core/partition, but may
include lists with fewer than n items at the end."
([n coll]
(partition-all n n coll))
([n step coll]
(lazy-seq
(when-let [s (seq coll)]
(cons (take n s) (partition-all n step (drop step s)))))))

(defn shuffle
"Return a random permutation of coll"
[coll]
(let [l (java.util.ArrayList. coll)]
(java.util.Collections/shuffle l)
(seq l)))

(defn rand-elt
"Return a random element of this seq"
[s]
(nth s (rand-int (count s))))


;; seq-on written by Konrad Hinsen
(defmulti seq-on
"Returns a seq on the object s. Works like the built-in seq but as
Expand Down

0 comments on commit 78ee9b3

Please sign in to comment.