Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
174 lines (145 sloc) 5.25 KB
;;; seq_utils.clj -- Sequence utilities for Clojure
;; by Stuart Sierra,
;; last updated March 2, 2009
;; Copyright (c) Stuart Sierra, 2008. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (
;; which can be found in the file epl-v10.html at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
;; Change Log
;; 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
#^{:author "Stuart Sierra (and others)",
:doc "Sequence utilities for Clojure"}
;; 'flatten' written by Rich Hickey,
;; see
(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."
(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.
(indexed '(a b c d)) => ([0 a] [1 b] [2 c] [3 d])"
(map vector (iterate inc 0) s))
;; group-by written by Rich Hickey;
;; see
(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]
(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)))]
(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."
(reduce (fn [counts x]
(assoc counts x (inc (get counts x 0))))
{} coll))
;; recursive sequence helpers by Christophe Grand
;; see
(defmacro rec-seq
"Similar to lazy-seq but binds the resulting seq to the supplied
binding-name, allowing for recursive expressions."
[binding-name & body]
`(let [s# (atom nil)]
(reset! s# (lazy-seq (let [~binding-name @s#] ~@body)))))
(defmacro rec-cat
"Similar to lazy-cat but binds the resulting sequence to the supplied
binding-name, allowing for recursive expressions."
[binding-name & exprs]
`(rec-seq ~binding-name (lazy-cat ~@exprs)))
;; reductions by Chris Houser
;; see
(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"
(if (seq x)
(fn [n _]
(lazy-cat (drop n x) (take n x)))
(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]
(when-let [s (seq coll)]
(cons (take n s) (partition-all n step (drop step s)))))))
(defn shuffle
"Return a random permutation of coll"
(let [l (java.util.ArrayList. coll)]
(java.util.Collections/shuffle l)
(seq l)))
(defn rand-elt
"Return a random element of this seq"
(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
a multimethod that can have implementations for new classes and types."
{:arglists '([s])}
(defmethod seq-on :default
(seq s))
(defn find-first
"Returns the first item of coll for which (pred item) returns logical true.
Consumes sequences up to the first match, will consume the entire sequence
and return nil if no match is found."
[pred coll]
(first (filter pred coll)))
Jump to Line
Something went wrong with that request. Please try again.