Commit e7be9861 authored by NicolasPetton's avatar NicolasPetton
Browse files

Make seq.el more extensible by using cl-defmethod

* lisp/emacs-lisp/seq.el: Define seq.el functions using cl-defmethod to
make it easier to extend seq.el with new "seq types".
* test/automated/seq-tests.el (test-setf-seq-elt): New test.
* lisp/emacs-lisp/cl-extra.el (cl-subseq): Move back the definition of
subseq in cl-extra.el, and use it in seq.el.
parent 58c3762a
......@@ -38,7 +38,6 @@
;;; Code:
(require 'cl-lib)
(require 'seq)
;;; Type coercion.
......@@ -520,13 +519,32 @@ This sets the values of: `cl-most-positive-float', `cl-most-negative-float',
If END is omitted, it defaults to the length of the sequence.
If START or END is negative, it counts from the end.
Signal an error if START or END are outside of the sequence (i.e
too large if positive or too small if negative)"
too large if positive or too small if negative)."
(declare (gv-setter
(lambda (new)
(macroexp-let2 nil new new
`(progn (cl-replace ,seq ,new :start1 ,start :end1 ,end)
(seq-subseq seq start end))
(cond ((or (stringp seq) (vectorp seq)) (substring seq start end))
((listp seq)
(let (len
(errtext (format "Bad bounding indices: %s, %s" start end)))
(and end (< end 0) (setq end (+ end (setq len (seq-length seq)))))
(if (< start 0) (setq start (+ start (or len (setq len (seq-length seq))))))
(unless (>= start 0)
(error "%s" errtext))
(when (> start 0)
(setq seq (nthcdr (1- start) seq))
(or seq (error "%s" errtext))
(setq seq (cdr seq)))
(if end
(let ((res nil))
(while (and (>= (setq end (1- end)) start) seq)
(push (pop seq) res))
(or (= (1+ end) start) (error "%s" errtext))
(nreverse res))
(seq-copy seq))))
(t (error "Unsupported sequence: %s" seq))))
(defalias 'cl-concatenate #'seq-concatenate
This diff is collapsed.
......@@ -54,6 +54,11 @@ Evaluate BODY for each created sequence.
"Return t if INTEGER is odd."
(not (test-sequences-evenp integer)))
(ert-deftest test-setf-seq-elt ()
(with-test-sequences (seq '(1 2 3))
(setf (seq-elt seq 1) 4)
(should (= 4 (seq-elt seq 1)))))
(ert-deftest test-seq-drop ()
(with-test-sequences (seq '(1 2 3 4))
(should (equal (seq-drop seq 0) seq))
......@@ -192,7 +197,7 @@ Evaluate BODY for each created sequence.
(should-error (seq-subseq [] -1))
(should-error (seq-subseq "" -1))
(should-not (seq-subseq '() 0))
(should-error(seq-subseq '() 0 -1)))
(should-error (seq-subseq '() 0 -1)))
(ert-deftest test-seq-concatenate ()
(with-test-sequences (seq '(2 4 6))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment