Elisp: More Sequence Functions

By Xah Lee. Date: . Last updated: .

Restructure, Reshape

seq-partition
(seq-partition (number-sequence 1 7 ) 2)
;; ((1 2) (3 4) (5 6) (7))
seq-split

note: seq-split and seq-partition seems to be mostly identical.

(seq-split (number-sequence 1 7) 2)
;; ((1 2) (3 4) (5 6) (7))
seq-group-by

(seq-group-by FUNCTION SEQUENCE)

  • Group items into a Association List .
  • The return value of FUNCTION is used as the key.
(setq xx
      (seq-group-by
       (lambda (x)
         (cond
          ((equal (mod x 2) 0) "2")
          ((equal (mod x 3) 0) "3")
          ((equal (mod x 5) 0) "5")
          (t "other")))
       (number-sequence 1 30)))

;; (
;; ("5" 5 25)
;; ("3" 3 9 15 21 27)
;; ("other" 1 7 11 13 17 19 23 29)
;; ("2" 2 4 6 8 10 12 14 16 18 20 22 ...)
;; )

(assoc "3" xx)
;; ("3" 3 9 15 21 27)

misc

seq-random-elt

(seq-random-elt SEQUENCE)

Return a randomly element.

(seq-random-elt [3 4 5])

Min, Max

seq-min
get the smallest number.
seq-max
Get the biggest number.

Elisp, sequence functions