Elisp: Sequence. Split, Partition, Group by
Split, Partition
seq-split-
(seq-split SEQUENCE LENGTH)split SEQUENCE into sub lists, each with length LENGTH, except the last one which may be shorter.
(seq-split (number-sequence 1 7) 2) ;; ((1 2) (3 4) (5 6) (7)) ;; second arg cannot be negative (seq-split (number-sequence 1 7) -1) ;; Debugger entered--Lisp error: (error "Sub-sequence length must be larger than zero")
seq-partition-
seq-splitandseq-partitionseems to be the same.They are implemented in different ways, but end result seems same, except when second arg is negative,
seq-spliterrors out butseq-partitionreturnnil.Also,
seq-splitis defined bydefun.seq-partitionis defined viacl-defgeneric. Theseq-splitseems a bug, because all function names starting with "seq-" is meant to be generic that works on future new data type of sequence.(seq-partition (number-sequence 1 7 ) 2) ;; ((1 2) (3 4) (5 6) (7))
Group into association list
seq-group-by-
(seq-group-by FUNCTION SEQUENCE)- Group items into a Association List.
- The return value of FUNCTION is used as the key. (if different items result in the same key, they are grouped together.)
;; group a list of 1 to 20 by even and odd (setq xx (seq-group-by (lambda (x) (if (eq (mod x 2) 0) "even" "odd")) (number-sequence 1 20))) ;; (("odd" 1 3 5 7 9 11 13 15 17 19) ("even" 2 4 6 8 10 12 14 16 18 20)) (assoc "even" xx) ;; ("even" 2 4 6 8 10 12 14 16 18 20)