From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Joost Kremers Newsgroups: gmane.emacs.help Subject: Re: Faking local dynamic function bindings Date: 29 Jan 2014 09:05:23 GMT Message-ID: References: NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1390986612 31146 80.91.229.3 (29 Jan 2014 09:10:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 29 Jan 2014 09:10:12 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jan 29 10:10:20 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1W8RA4-0008Kg-74 for geh-help-gnu-emacs@m.gmane.org; Wed, 29 Jan 2014 10:10:20 +0100 Original-Received: from localhost ([::1]:41114 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8RA3-0003hM-Oo for geh-help-gnu-emacs@m.gmane.org; Wed, 29 Jan 2014 04:10:19 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 59 Original-X-Trace: individual.net BZUtwSkENWCqO2gJESqWVAbFzT8nrMrqx2A/GQo3lXBizMD1aH Cancel-Lock: sha1:553re2FR6sjqoXF9uSP49ZyuWQ4= Mail-Copies-To: nobody X-Editor: Emacs of course! User-Agent: slrn/pre1.0.0-18 (Linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:203478 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:95747 Archived-At: EN:SiS(9) Stefan Monnier wrote: >> (defun foo (sexpr) >> (eval >> `(cl-macrolet ((contains (regexp) >> `(string-match ,regexp string))) >> (sort (delq nil (mapcar #'(lambda (item) >> (let ((string (assq item strings-alist))) >> (when ,sexpr) >> string)) >> items-alist)) >> 'string<)))) > > IIUC, sexpr is only known very late (like much later than when you > byte-compile this file). Yes, you understand correctly. > If that's the case, there's no avoiding `eval'. This is the point where I hit myself over the head and say duh... That should have been obvious, even to me. > But you can move the eval closer to sexpr: > > (defun foo (sexpr) > (sort (delq nil (mapcar #'(lambda (item) > (let ((string (assq item strings-alist))) > (when (eval > `(cl-macrolet ((contains (regexp) > `(string-match ,regexp ,string))) > ,sexpr)) > string)) > items-alist)) > #'string<))) This resembles an earlier version of my code, but AFAIU it has the disadvantage that the local macro binding is created and destroyed every time the anonymous function is executed, which I guess creates extra overhead and makes the function slower. That's why I moved the cl-macrolet outside the mapcar. (Though I haven't done any profiling and the older version was fast enough not to be annoying...) > And of course, you don't need `contains' to be a macro: True, of course. However, I'm using a macro because in the actual code one of the arguments of `contains` is an unquoted symbol. (That's an unfortunate design decision I made years ago that has proven to be completely pointless and impractical. I've been meaning to change it, but haven't found the time to do so yet.) Thanks, Joost -- Joost Kremers joostkremers@fastmail.fm Selbst in die Unterwelt dringt durch Spalten Licht