Elisp: Sequence Map Insert

By Xah Lee. Date: . Last updated: .

Insert / Delete / Modify Items (At Arbitrary Positions)

seq-mapcat

(seq-mapcat FUNCTION SEQUENCE &optional TYPE)

Apply a function that return a list (can be empty list), but insert its elements into the sequence.

  • if the function return a empty list (nil), the item is removed.
  • if the function return a list of single item, the item is replaced.
  • if the function return a non-empty list, the items are inserted.

💡 TIP: This function is more general than map because map cannot delete items, and more general than filter because filter cannot add item nor transform the items you want.

(setq xx (vector "aa" "bb" "m,n" "cc" ))

;; if the string contains a comma, split it into 2 items

(seq-mapcat
 (lambda (x)
   (if (string-match "," x)
       (split-string x ",")
     (list x)))
 xx)
;; ("aa" "bb" "m" "n" "cc")

Elisp, sequence functions