unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to create a higher order function?
@ 2021-09-21  4:10 Marcin Borkowski
  2021-09-21  4:50 ` Emanuel Berg via Users list for the GNU Emacs text editor
                   ` (4 more replies)
  0 siblings, 5 replies; 27+ messages in thread
From: Marcin Borkowski @ 2021-09-21  4:10 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all,

assume that I want a function which "inverts" the meaning of a given
predicate.  Something that would turn `<' into `>=' etc.

Here's my first attempt (A):

--8<---------------cut here---------------start------------->8---
;;; -*- lexical-binding: nil; -*-
(defun negate (fun)
  "Try to return a function returning the logical opposite of FUN."
  (lambda (&rest args)
    (not (apply fun args))))
--8<---------------cut here---------------end--------------->8---

Of course, it doesn't work, since (under dynamic scope) it just creates
a function which uses a dynamic binding for `fun' (which is probably
nil).

So, here's a better (at least: working) version (B):

--8<---------------cut here---------------start------------->8---
;;; -*- lexical-binding: nil; -*-
(defun negate (fun)
  "Return a function returning the logical opposite of FUN."
  `(lambda (&rest args)
    (not (apply ,(symbol-function fun) args))))
--8<---------------cut here---------------end--------------->8---

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):

--8<---------------cut here---------------start------------->8---
;;; -*- lexical-binding: t; -*-
(defun negate (fun)
  "Try to return a function returning the logical opposite of FUN."
  (lambda (&rest args)
    (not (apply fun args))))
--8<---------------cut here---------------end--------------->8---

My question is: which of (B) and (C) is better?  (C) is definitely
simpler, and with lexical scope becoming the default (perhaps at some
point in time in the future) or the recommended (even now, I guess)
setting, is probably the way to go.  But maybe (B) is better under some
circumstances?  Is it faster?  (I don't think so, and my simple
benchmarks were inconclusive.)  Is it more memory-efficient?  (Could
be.)  Does it have any other advantage?

Disclaimer: I'd like to study the replies and put the answer on my blog
and in my book.  I'll give the credit where it's due, of course.

TIA,

--
Marcin Borkowski
http://mbork.pl



^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2021-10-05  9:53 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-21  4:10 How to create a higher order function? Marcin Borkowski
2021-09-21  4:50 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-21  5:06 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-21 11:27 ` Leo Butler
2021-09-21 11:48   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-21 20:31     ` mm-uu-extract was: " Leo Butler
2021-09-21 22:11       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-24  9:02       ` Marcin Borkowski
2021-09-24  9:00   ` Marcin Borkowski
2021-09-21 15:49 ` [External] : " Drew Adams
2021-09-24  9:04   ` Marcin Borkowski
2021-09-24  9:38     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-24 16:50       ` Drew Adams
2021-09-24 17:11         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-25  6:36           ` Marcin Borkowski
2021-09-25 23:48             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-27 10:10               ` Marcin Borkowski
2021-09-28  1:41                 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28 17:14                   ` Marcin Borkowski
2021-09-29  4:28                     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-05  7:25                       ` Marcin Borkowski
2021-10-05  7:38                         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-05  9:53                           ` dlet/let/slet (was: Re: [External] : How to create a higher order function?) Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-22 19:03 ` How to create a higher order function? Stefan Monnier via Users list for the GNU Emacs text editor
2021-09-22 20:53   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-22 23:50   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-24  8:57   ` Marcin Borkowski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).