Elisp: Apply Function, funcall, identity

By Xah Lee. Date: . Last updated: .

Apply

apply

(apply FUNCTION &rest args_or_list)

Call FUNCTION with args_or_list.

If the last argument in args_or_list is a list, it is flattened out and feed as args to the function.

💡 TIP: this is useful useful when you have a list and want to feed them as arguments to a function.

;; sample function
(defun ff (x y z)
  "return args as one string"
  (format "%s %s %s" x y z))

;; normal usage
(ff 2 3 4)
;; "2 3 4"

;; s------------------------------

;; use apply, to turn a list into args
(apply 'ff (list 2 3 4))
;; "2 3 4"

;; s------------------------------

;; multi arg example
(apply 'ff 2 (list 3 4))
;; "2 3 4"

funcall

funcall

(funcall FUNCTION &rest args)

Call FUNCTION with args.

💡 TIP: this is useful when the function name is a variable, value known only at run-time.

;; test function
(defun ff (x y z)
  "return args as one string"
  (format "%s %s %s" x y z))

;; normal usage
(ff 2 3 4)
;; "2 3 4"

;; use funcall
(funcall 'ff 2 3 4)
;; "2 3 4"

identity, ignore, always

identity

(identity ARGUMENT)

Return the ARGUMENT unchanged.

💡 TIP: Identity function is common in functional programing languages. Often, function takes function, and you want a do-nothing function. This is most useful for mapconcat. 〔see Elisp: Join Strings, Codepoints to String

(identity "abc")
;; "abc"
ignore

(ignore &rest ARGUMENTS)

Ignore ARGUMENTS, do nothing, and return nil.

(ignore 1 2 3)
;; nil
always

(always &rest ARGUMENTS)

Ignore ARGUMENTS, do nothing, and return t.

(always 1 2 3)
;; t

Reference

Elisp, Function