Function: seq-subseq

seq-subseq is a byte-compiled function defined in seq.el.gz.

Signature

(seq-subseq SEQUENCE START &optional END)

Documentation

Return the sequence of elements of SEQUENCE from START to END.

END is exclusive.

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).

Other relevant functions are documented in the sequence and vector groups.

View in manual

Shortdoc

;; vector
(seq-subseq [1 2 3 4 5] 1 3)
    => [2 3]
  (seq-subseq [1 2 3 4 5] 1)
    => [2 3 4 5]
;; sequence
(seq-subseq '(a b c d e) 2 4)
    => (c d)

Aliases

widget-sublist (obsolete since 28.1) filesets-sublist (obsolete since 28.1)

Implementations

(seq-subseq SEQUENCE START &optional END) in `seq.el'.

Undocumented

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/seq.el.gz
;;;###autoload
(cl-defgeneric seq-subseq (sequence start &optional end)
  "Return the sequence of elements of SEQUENCE from START to END.
END is exclusive.

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)."
  (cond
   ((or (stringp sequence) (vectorp sequence)) (substring sequence start end))
   ((listp sequence)
    (let (len
          (orig-start start)
          (orig-end end))
      (and end (< end 0) (setq end (+ end (setq len (length sequence)))))
      (if (< start 0) (setq start (+ start (or len (setq len (length sequence))))))
      (unless (>= start 0)
        (error "Start index out of bounds: %s" orig-start))
      (when (> start 0)
        (setq sequence (nthcdr (1- start) sequence))
        (unless sequence
          (error "Start index out of bounds: %s" orig-start))
        (setq sequence (cdr sequence)))
      (if end
          (let ((n (- end start)))
            (when (or (< n 0)
                      (if len
                          (> end len)
                        (and (> n 0) (null (nthcdr (1- n) sequence)))))
              (error "End index out of bounds: %s" orig-end))
            (take n sequence))
        (copy-sequence sequence))))
   (t (error "Unsupported sequence: %s" sequence))))