unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Madhu <enometh@meer.net>
To: platon7pronko@gmail.com
Cc: help-gnu-emacs@gnu.org
Subject: Re: the (declare special) declaration with lexical scope.
Date: Mon, 24 Apr 2023 07:46:01 +0530 (IST)	[thread overview]
Message-ID: <20230424.074601.1016279240196114273.enometh@meer.net> (raw)
In-Reply-To: <d4e72287-61c4-129c-9886-c0881237b9da@gmail.com>

*  Platon Pronko <d4e72287-61c4-129c-9886-c0881237b9da@gmail.com>
Wrote on Sun, 23 Apr 2023 17:47:40 +0800
> However I'm interested why you didn't consider to make the entire
> mustache function into a macro that expands to concat call with
> variables already interpolated?  This way it will work regardless of
> lexical or dynamic binding.

You're right. If I had started that way it lexenv wouldn't have been
an issue and my goal here was mainly to get the "rendering context"
from the (dynamic or lexical) environment.

But I imagined the idea of "templating" involves compiling a template
pattern to a funcallable and calling it repeatedly with different
arguments.


> I suppose this might break if template is dynamically generated
> somewhere, however templates are usually static and thus can be
> embedded at compile time (it will be the fastest-performing option,
> aside from other benefits).
>
> Here's an example of what I mean:
>
> (defun mustache-build (template)
>   (let ((pattern-start (string-match "{{\\(.*?\\)}}" template)))
>     (if (not pattern-start)
>         template
>       `(,(substring template 0 pattern-start)
>         (prin1-to-string ,(intern (match-string 1 template)))
>         ,@(if (>= (match-end 0) (length template)) nil (mustache-build
>         (substring template (match-end 0))))))))
> (defmacro mustache (template)
>   `(concat ,@(mustache-build template)))
>
> (macroexpand '(mustache "a={{a}} b={{b}}"))
> ; (concat "a=" (prin1-to-string a) " b=" (prin1-to-string b))
>
> (let ((a 42) (b '(+ 2 3)))
>   (mustache "a={{a}} b={{b}}"))
> ; "a=42 b=(+ 2 3)"


This is enough for the problem I was working on.



  reply	other threads:[~2023-04-24  2:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-23  5:07 the (declare special) declaration with lexical scope Madhu
2023-04-23  9:47 ` Platon Pronko
2023-04-24  2:16   ` Madhu [this message]
2023-04-23 14:22 ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230424.074601.1016279240196114273.enometh@meer.net \
    --to=enometh@meer.net \
    --cc=help-gnu-emacs@gnu.org \
    --cc=platon7pronko@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).