[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to create a higher order function?
From: |
Leo Butler |
Subject: |
Re: How to create a higher order function? |
Date: |
Tue, 21 Sep 2021 06:27:43 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) |
Marcin Borkowski <mbork@mbork.pl> writes:
> Hi all,
>
> assume that I want a function which "inverts" the meaning of a given
> predicate. Something that would turn `<' into `>=' etc.
>
> So, here's a better (at least: working) version (B):
>
> ;;; -*- lexical-binding: nil; -*-
> (defun negate (fun)
> "Return a function returning the logical opposite of FUN."
> `(lambda (&rest args)
> (not (apply ,(symbol-function fun) args))))
I think negate would need to be a defmacro in this case, not a defun.
This construction, as it is, would return a list, not a lambda function.
`(foo ,X bar) is equivalent to (list 'foo X 'bar).
>
>
> It seems to work fine, even if it's a bit complicated.
>
> Of course, the more obvious way to do it is to use the first definition
> under lexical scope (C):
>
> ;;; -*- lexical-binding: t; -*-
> (defun negate (fun)
> "Try to return a function returning the logical opposite of FUN."
> (lambda (&rest args)
> (not (apply fun args))))
Obviously C is better.
Q: I see you use mu4e. How do you get the code highlighted? (in my Gnus,
the background is distinctive blue).
Leo