Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to write function to compose functions in Elisp?

Tags:

emacs

elisp

I wanted to create a function, that returns a composition of several other functions, such that

(funcall (compose 'f 'g) x) == (f (g x))

I feel that I miserably failed on this. My best attempt so far:

(defun compose (funcs)
  "composes several funcitons into one"
  (lambda (arg)
    (if funcs
        (funcall (car funcs) (funcall (compose (cdr funcs)) arg))
        arg)))

But for some reason, the following still returns 0:

(funcall (compose '(
           (lambda (a) (* a 3))
           (lambda (a) (+ a 2))
)) 0)

Is there a way to fix that?

like image 340
Rogach Avatar asked Nov 01 '12 09:11

Rogach


1 Answers

Your code requires lexically scoped lambdas, which Emacs Lisp doesn't support by default. If you use Emacs 24, set lexical-binding to t and your example will work as written.

If you're using an older Emacs, you can create a lexically scoped lambda using an explicit lexical-let form:

(require 'cl)

(defun compose (funcs)
  "composes several funcitons into one"
  (lexical-let ((funcs funcs))
    (lambda (arg)
      (if funcs
          (funcall (car funcs) (funcall (compose (cdr funcs)) arg))
        arg))))
like image 192
user4815162342 Avatar answered Sep 18 '22 03:09

user4815162342