Elisp: Pipe Function (Function Chain, Composition) 📜
Pipe Function
Here's a function that does piping functions (aka function chain).
;; -*- coding: utf-8; lexical-binding: t; -*- (defun xah-pipe (zinit &rest zfunctions) "Apply functions to a value. (xah-pipe x f g h) computes (h (g (f x))) Created: 2025-12-25 Version: 2025-12-25" (let ((xstate zinit)) (while zfunctions (setq xstate (funcall (pop zfunctions) xstate))) xstate)) ;; s------------------------------ ;; test (xah-pipe "abc" (lambda (x) (string-replace "a" "1" x)) (lambda (x) (string-replace "b" "2" x)) (lambda (x) (string-replace "c" "3" x))) ;; "123"
One major problem of lisp is that it doesn't have pipe function. This is essential in functional programing. You can write one, but that is not practical, because then every time you use it you have to include the code, or from a lib. Creates a dependency problem.