Elisp: Sequence. some, every (conditional exit)

By Xah Lee. Date: . Last updated: .

Foreach with Break, Get F Return Value

seq-some

(seq-some PRED SEQUENCE)

  • Apply a function PRED to each sequence element in order.
  • Exit loop when PRED return true. Return that value.
  • If no result is true, return false.

💡 TIP: This is useful as functional form of loop with break, especially if you want the return value of the PRED.

;; demo of seq-some
(seq-some
 (lambda (x)
   (if (eq 5 x)
       x
     nil
     ))
 [4 5 6])
;; 5

Foreach with Break

seq-every-p

(seq-every-p PRED SEQUENCE)

  • Apply a function PRED to each sequence element in order.
  • Exit loop when PRED return false.
  • Return true if every result is true, else false.

💡 TIP: This is useful as functional form of loop with break, and you no need any return value.

;; demo of seq-every-p
(seq-every-p
 (lambda (x)
   (if (eq (mod x 2) 0)
       x
     nil
     ))
 [4 5 6])
;; nil

Elisp, sequence functions