all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Artur Malabarba <arturmalabarba@gmail.com>
To: emacs-devel <emacs-devel@gnu.org>
Subject: Re: New assoc-let package
Date: Fri, 5 Dec 2014 20:28:29 +0000	[thread overview]
Message-ID: <CAAdUY-L=XeaXKOL-jWn7WBiAa8J8etDsrOipVV3-tkSVzRkFnA@mail.gmail.com> (raw)
In-Reply-To: <CAAdUY-KTdsqz0jOE9u6iyWfvB4WRQPi8K1_v2xe+09KYAePkug@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1317 bytes --]

And of course I forgot to attach.
Artur Malabarba

School of Physics,
University of Bristol, UK
(+44) 07450 374440


2014-12-05 20:27 GMT+00:00 Artur Malabarba <arturmalabarba@gmail.com>:
> On 5 Dec 2014 00:45, "Ted Zlatanov" <tzz@lifelogs.com> wrote:
>> >> Should it maybe use `lexical-let'?
>>
>> AM> Maybe. Do you foresee it behaving unexpectedly in a file with dynamic
>> AM> scope?
>>
>> If you want to support older Emacsen, it's probably better. But it's up
>> to you, and I don't know exactly what your code does.
>
>> >> I hope it can go into the core so it's available by default.
>>
>> AM> On the other hand, making it a package makes it a reliable dependency
>> AM> available since 24.1
>>
>> To me, a package seems overkill for a single useful macro, but I haven't
>> seen the source so perhaps there's more to it. There was no URL in your
>> post and I don't see an assoc-let package in any of the ELPA repos I
>> know.
>
> Yes, when I sent that email I hadn't yet factored it out from the other
> project where it was born.
>
> I've attached the file now. There isn't much more to it, it's really quite
> simple.
>
>
>> Anyhow, it's your code, please publish it however you wish.
>
> Of course, but all feedback is welcome.
>
> I'll be pushing this once I've written a test or two for it.
>
>
> Cheers

[-- Attachment #2: assoc-let.el --]
[-- Type: text/x-emacs-lisp, Size: 3375 bytes --]

;;; assoc-let.el --- Easily let-bind values of an alist by their names.

;; Copyright (C) 2014 Free Software Foundation, Inc.

;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
;; Maintainer: Artur Malabarba <bruce.connor.am@gmail.com>
;; URL: http://github.com/Bruce-Connor/assoc-let
;; Version: 0.1
;; Keywords: extensions lisp
;; Prefix: assoc-let
;; Separator: -

;;; Commentary:
;;
;; This package offers a single macro, `assoc-let'. This macro takes a
;; first argument (whose value must be an alist) and a body.
;; 
;; The macro expands to a let form containing body, where each dotted
;; symbol inside body is let-bound to their cdrs in the alist.  Dotted
;; symbol is any symbol starting with a `.'.  Only those present in
;; the body are let-bound and this search is done at compile time.
;; 
;; For instance, the following code
;; 
;;   (assoc-let alist
;;     (if (and .title .body) 
;;         .body
;;       .site))
;; 
;; expands to
;; 
;;   (let ((.title (cdr (assoc 'title alist)))
;;         (.body (cdr (assoc 'body alist)))
;;         (.site (cdr (assoc 'site alist))))
;;     (if (and .title .body)
;;         .body
;;       .site))

;;; License:
;;
;; This file is part of GNU Emacs.
;;
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; News:
;;; Code:
\f

(defun assoc-let--deep-dot-search (data)
  "Return alist of symbols inside DATA which start with a `.'.
Perform a deep search and return an alist where each car is the
symbol, and each cdr is the same symbol without the `.'."
  (cond
   ((symbolp data)
    (let ((name (symbol-name data)))
      (when (string-match "\\`\\." name)
        ;; Return the cons cell inside a list, so it can be appended
        ;; with other results in the clause below.
        (list (cons data (intern (replace-match "" nil nil name)))))))
   ((not (listp data)) nil)
   (t (apply #'append
        (remove nil (mapcar #'assoc-let--deep-dot-search data))))))

;;;###autoload
(defmacro assoc-let (alist &rest body)
  "Let-bind dotted symbols to their cdrs in ALIST and execute BODY.
Dotted symbol is any symbol starting with a `.'.  Only those
present in BODY are let-bound and this search is done at compile
time.

For instance, the following code

  (assoc-let alist
    (if (and .title .body) 
        .body
      .site))

expands to

  (let ((.title (cdr (assoc 'title alist)))
        (.body (cdr (assoc 'body alist)))
        (.site (cdr (assoc 'site alist))))
    (if (and .title .body)
        .body
      .site))"
  (declare (indent 1) (debug t))
  `(let ,(mapcar (lambda (x) `(,(car x) (cdr (assoc ',(cdr x) ,alist))))
                 (delete-dups (assoc-let--deep-dot-search body)))
     ,@body))

(provide 'assoc-let)
;;; assoc-let.el ends here


;; Local Variables:
;; sentence-end-double-space: t
;; End:

  reply	other threads:[~2014-12-05 20:28 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-03 23:40 New assoc-let package Artur Malabarba
2014-12-04  3:54 ` Stefan Monnier
2014-12-05  0:36   ` Artur Malabarba
2014-12-05  2:49     ` Leo Liu
2014-12-06  2:27       ` Ted Zlatanov
2014-12-04 19:36 ` Ted Zlatanov
2014-12-05  0:33   ` Artur Malabarba
2014-12-05  0:45     ` Ted Zlatanov
2014-12-05 20:27       ` Artur Malabarba
2014-12-05 20:28         ` Artur Malabarba [this message]
2014-12-06 11:36           ` Christopher Schmidt
2014-12-06 19:24             ` Artur Malabarba
2014-12-06 19:58               ` Christopher Schmidt
2014-12-07 16:25                 ` Ted Zlatanov
2014-12-06  2:42         ` Ted Zlatanov
2014-12-06  4:55           ` Stefan Monnier
2014-12-08 10:56             ` Ted Zlatanov
2014-12-08 12:11               ` Artur Malabarba
2014-12-08 14:01                 ` Ted Zlatanov
2014-12-08 15:01                 ` Stefan Monnier
2014-12-08 14:59               ` Stefan Monnier
2014-12-08 15:35                 ` Ted Zlatanov
2014-12-08 15:56                 ` Stefan Monnier
2014-12-08 18:23                 ` Artur Malabarba
2014-12-08 18:28                   ` Artur Malabarba
2014-12-08 20:07                     ` Stefan Monnier
2014-12-09  3:37                       ` Artur Malabarba
2014-12-09 16:57                         ` Stefan Monnier
2014-12-10 15:20             ` Ted Zlatanov
2014-12-10 15:43               ` Stefan Monnier
2014-12-10 15:49                 ` Ted Zlatanov
2014-12-10 18:28                   ` Artur Malabarba
2014-12-10 19:00                     ` Ted Zlatanov
2014-12-10 19:09                   ` Stefan Monnier
2014-12-10 19:14                     ` Ted Zlatanov

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

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

  git send-email \
    --in-reply-to='CAAdUY-L=XeaXKOL-jWn7WBiAa8J8etDsrOipVV3-tkSVzRkFnA@mail.gmail.com' \
    --to=arturmalabarba@gmail.com \
    --cc=emacs-devel@gnu.org \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.