This package is now both in Emacs 25.1 and ELPA. Further development will only happen there.
Do not send pull requests or open issues here (use M-x report-emacs-bug
instead).
seq.el provides Sequence manipulation functions that complement basic
functions provided by subr.el
. The library is included in Emacs 25.
All functions are prefixed with seq-
.
All provided functions work on lists, strings and vectors.
Functions taking a predicate or iterating over a sequence using a function as argument take the function as their first argument and the sequence as their second argument. All other functions take the sequence as their first argument.
All functions are tested in test/seq-tests.el
Note: The version 2 of seq.el is maintained in the Emacs git repository, please consider sending patches there.
seq.el provides the following functions:
-
seq-drop
seq nThis function returns a sequence of all but the first
n
elements of the sequenceseq
.seq
may be a list, vector or string andn
must be an integer. The result is the same type of sequence asseq
.If
n
is a negative integer or zero,seq
is returned. -
seq-take
seq nThis function returns a sequence of the first
n
elements ofseq
.seq
may be a list, vector or string andn
must be an integer. The result is the same type of sequence asseq
.If
n
is a negative integer or zero, an empty sequence is returned. -
seq-take-while
pred seqThis function returns a sub-sequence of the successive elements of
seq
for which callingpred
with that element returns non-nil.pred
must be a one-argument function andseq
may be a list, vector or string. The result is the same type of sequence asseq
.If evaluating
pred
with the first element ofseq
as argument returnsnil
, an empty sequence is returned. -
seq-drop-while
pred seqThis function returns a sub-sequence of
seq
from the first element for which callingpred
with that element returnsnil
.pred
must be a one-argument function andseq
may be a list, vector or string. The result is the same type of sequence asseq
.If evaluating
pred
with every element ofseq
returnsnil
,seq
is returned. -
seq-filter
pred seqThis function returns a list of all the elements in
seq
for which callingpred
with that element returns non-nil.pred
must be a one-argument function andseq
may be a list, vector or string. -
seq-map
function seqThis function returns the result of applying
function
to each element ofseq
. The returned value is a list.function
must be a one-argument function andseq
may be a list, vector or string. -
seq-mapn
function &rest seqsThis function returns the result of applying
function
to each element of allseqs
. The returned value is a list.The arity of FUNCTION must match the number of SEQS, and the mapping stops on the shortest sequence.
-
seq-remove
pred seq This function returns a list of all the elements inseq
for which callingpred
with that element returnsnil
.pred
must be a one-argument function andseq
may be a list, vector or string. -
seq-reduce
function seq initial-value This function returns the result of callingfunction
withinitial-value
and the first element ofseq
, then callingfunction
with that result and the second element ofseq
, then with that result and the third element ofseq
, etc.function
must be a two-arguments function andseq
may be a list, vector or string.If
seq
is empty,initial-value
is returned andfunction
is not called. -
seq-some
pred seq This function returns non-nil if callingpred
with any element ofseq
returns non-nil. If so, the returned value is the value returned byseq
.pred
must be a one-argument function andseq
may be a list, vector or string. -
seq-find
pred seq &optional default This function returns the first element for whichpred
returns non-nil inseq
. If no element matchespref
,default
is returned.Note that this function has an ambiguity if the found element is identical to
default
, as it cannot be known if an element was found or not. -
seq-every-p
pred seq This function returns non-nil if successively callingpred
with each element ofseq
always returns non-nil,nil
otherwise.pred
must be a one-argument function andseq
may be a list, vector or string. -
seq-empty-p
seq This function returns non-nil if the sequenceseq
is empty,nil
otherwise.seq
may be a list, vector or string. -
seq-count
pred seq This function returns the number of elements inseq
for which callingpred
with that element returns non-nil.pred
must be a one-argument function andseq
may be a list, vector or string. -
seq-sort
pred seq This function returns a sorted sequence of the elements ofseq
, comparing its elements withpred
. Called with two elements ofseq
,pred
should return non-nil if the first element should sort before the second.pred
must be a two-arguments function,seq
may be a list, vector or string.The result is a sequence of the same type as SEQ.
-
seq-contains
seq elt testfn This function returns the first element inseq
that equals toelt
.Equality is defined by
testfn
if non-nil or byequal
ifnil
.seq
may be a list, vector or string. -
seq-position
seq elt testfn This function returns the index of the first element inseq
that is equal toelt
. Equality is defined bytestfn
if non-nil or byequal
if nil."seq
may be a list, vector or string. -
seq-uniq
seq testfn This function returns a list of the elements ofseq
with duplicates removed.testfn
is used to compare elements, orequal
iftestfn
isnil
.testfn
must be a two-argument function ornil
andseq
may be a list, vector or string. -
seq-subseq
seq start &optional end This function returns a sub-sequence ofseq
fromstart
toend
. Ifend
is omitted, it default to the length ofseq
. Ifstart
orend
is negative, it counts from the end ofseq
.seq
may be a list, vector or string. The result is the same type of sequence asseq
. -
seq-concatenate
type &rest seqs This function returns a sequence made of the concatenation ofseqs
. The result is a sequence of typetype
.type
may be one of the following symbols:vector
,list
orstring
. -
seq-mapcat
function sequence &optional type This function returns the result of applyingseq-concatenate
to the result of applyingfunction
to each element ofsequence
. The result is a sequence of typetype
, or a list iftype
is nil. -
seq-partition
sequence n This function returns a list of the elements ofsequence
grouped into sub-sequences of lengthn
. The last sequence may contain less elements thann
.n
must be an integer. Ifn
is a negative integer or 0, nil is returned. -
seq-group-by
function sequence This function separates the elements ofsequence
into an alist whose keys are the result of applyingfunction
to each element ofsequence
. Keys are compared usingequal
. -
seq-intersection
seq1 seq2 &optional testfn Return a list of the elements that appear in bothseq1
andseq2
. Equality is defined by TESTFN if non-nil or byequal
if nil. -
seq-difference
seq1 seq2 &optional testfn Return a list of the elements that appear in bothseq1
but not inseq2
. Equality is defined by TESTFN if non-nil or byequal
if nil. -
seq-doseq
(var seq [result]) body... This macro is likedolist
, except thatseq
can be a list, vector or string.seq-doseq
is primarily useful for side-effects. -
seq-let
args &rest body Bind the variables in ARGS to the elements of SEQ then evaluate BODY. ARGS can also include the `&rest' marker followed by a variable name to be bound to the rest of SEQ.
Since this library is in Emacs 25, contributors must have signed the Emacs Copyright assignment.
Fetch dependencies:
$ cd /path/to/seq.el
$ cask
Run the tests:
$ ./run-tests.sh