unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Clojure-like syntactic sugar for an anonymous function literal
@ 2015-01-21 21:38 Oleh
  2015-01-21 22:28 ` samer
                   ` (2 more replies)
  0 siblings, 3 replies; 78+ messages in thread
From: Oleh @ 2015-01-21 21:38 UTC (permalink / raw)
  To: emacs-devel

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

Hi all,

This is my first attempt of modifying the C source of Emacs.
The goal is to add a new reader syntax:

    #(foo bar) should translate to (short-lambda (foo bar))

This is in the same way that the `backquote' is working now:

    `(foo bar) translates to (backquote (foo bar))

The `short-lambda' macro is implemented here:
https://github.com/abo-abo/short-lambda.

In the simplest form, i.e. one that does not allow multiple or
&rest-style arguments, it can look like this:

    (defmacro short-lambda (x)
      `(lambda (%) ,x))

Even in this simple form, it can sweeten up Elisp a lot:

    (mapc #(put % 'disabled nil)
          '(upcase-region downcase-region narrow-to-region))

With the (less trivial) linked `short-lambda', the following is
possible:

    (cl-mapcar #(concat %1 " are " %2)
               '("roses" "violets")
               '("red" "blue"))

Here's a snippet from `org-mode':

    (mapcar (lambda (x)
              (and (member (car x) matchers) (nth 1 x)))
            org-latex-regexps)

Here's the sweetened code:

    (mapcar #(and (member (car %) matchers) (nth 1 %))
            org-latex-regexps)

As far as I know, only #("foo" ...) literal is already taken, so #(Z,
where Z is anything but a " is up for grabs, reader-wise.

I hope that you don't view this patch as malarkey. Saving a few chars
while typing and reading is a big deal to a lot of people.  And the
way I see it, this implementation doesn't cost much.

regards,
Oleh

[-- Attachment #2: 0001-Modify-reader-.-is-short-lambda.patch --]
[-- Type: text/x-patch, Size: 3436 bytes --]

From a8e0c5418598655bdfdda6d5cb541262a5575927 Mon Sep 17 00:00:00 2001
From: Oleh Krehel <ohwoeowho@gmail.com>
Date: Fri, 9 Jan 2015 08:33:07 +0100
Subject: [PATCH] Modify reader: #([^"]...) is (short-lambda ...)

* src/lread.c (read1): When "#(" is found, check if the next symbol is
  '"'. If it is, continue with the old string syntax. Otherwise, treat
  "#(...)" similar to a backquote, except the symbol name is
  `short-lambda' instead of `backquote'.

The indent is to allow to use "#(+ % %)" in place of
"(lambda (%) (+ % %))".

The simplest implementation:

    (defmacro short-lambda (structure)
      `(lambda (%) ,structure))

It could be easily extended to produce "(lambda (%1 %2) (+ %1 %2))"
from "#(+ %1 %2)".
---
 src/lread.c | 65 +++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 37 insertions(+), 28 deletions(-)

diff --git a/src/lread.c b/src/lread.c
index 3240524..905164f 100644
--- a/src/lread.c
+++ b/src/lread.c
@@ -2671,34 +2671,42 @@ read1 (Lisp_Object readcharfun, int *pch, bool first_in_list)
 	}
       if (c == '(')
 	{
-	  Lisp_Object tmp;
-	  struct gcpro gcpro1;
-	  int ch;
-
-	  /* Read the string itself.  */
-	  tmp = read1 (readcharfun, &ch, 0);
-	  if (ch != 0 || !STRINGP (tmp))
-	    invalid_syntax ("#");
-	  GCPRO1 (tmp);
-	  /* Read the intervals and their properties.  */
-	  while (1)
-	    {
-	      Lisp_Object beg, end, plist;
-
-	      beg = read1 (readcharfun, &ch, 0);
-	      end = plist = Qnil;
-	      if (ch == ')')
-		break;
-	      if (ch == 0)
-		end = read1 (readcharfun, &ch, 0);
-	      if (ch == 0)
-		plist = read1 (readcharfun, &ch, 0);
-	      if (ch)
-		invalid_syntax ("Invalid string property list");
-	      Fset_text_properties (beg, end, plist, tmp);
-	    }
-	  UNGCPRO;
-	  return tmp;
+          int next_char = READCHAR;
+          UNREAD (next_char);
+          if (next_char == '"') {
+            Lisp_Object tmp;
+            struct gcpro gcpro1;
+            int ch;
+
+            /* Read the string itself.  */
+            tmp = read1 (readcharfun, &ch, 0);
+            if (ch != 0 || !STRINGP (tmp))
+              invalid_syntax ("#");
+            GCPRO1 (tmp);
+            /* Read the intervals and their properties.  */
+            while (1)
+              {
+                Lisp_Object beg, end, plist;
+
+                beg = read1 (readcharfun, &ch, 0);
+                end = plist = Qnil;
+                if (ch == ')')
+                  break;
+                if (ch == 0)
+                  end = read1 (readcharfun, &ch, 0);
+                if (ch == 0)
+                  plist = read1 (readcharfun, &ch, 0);
+                if (ch)
+                  invalid_syntax ("Invalid string property list");
+                Fset_text_properties (beg, end, plist, tmp);
+              }
+            UNGCPRO;
+            return tmp;
+          } else {
+            UNREAD(c);
+            Lisp_Object value = read0 (readcharfun);
+            return list2 (Qshort_lambda, value);
+          }
 	}
 
       /* #@NUMBER is used to skip NUMBER following bytes.
@@ -4731,6 +4739,7 @@ that are loaded before your customizations are read!  */);
   DEFSYM (Qcomma, ",");
   DEFSYM (Qcomma_at, ",@");
   DEFSYM (Qcomma_dot, ",.");
+  DEFSYM (Qshort_lambda, "short-lambda");
 
   DEFSYM (Qinhibit_file_name_operation, "inhibit-file-name-operation");
   DEFSYM (Qascii_character, "ascii-character");
-- 
1.8.4


^ permalink raw reply related	[flat|nested] 78+ messages in thread
* Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal
@ 2015-01-26 22:22 Barry OReilly
  0 siblings, 0 replies; 78+ messages in thread
From: Barry OReilly @ 2015-01-26 22:22 UTC (permalink / raw)
  To: stephen; +Cc: emacs-devel

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

> (car #(fubAr quux)) => 'lambda
>
> would be *very* surprising to most users.  That kind of computation is
> what functions are for.

Taking the car of a function is not valid anyway.

Byte compile:

  (defvar myfunc (lambda ()))

Evaluate:

  (car myfunc)

Error:

  Debugger entered--Lisp error: (wrong-type-argument listp #[nil "\300\207"
[nil] 1])
    car(#[nil "\300\207" [nil] 1])
    eval((car (byte-compile (function (lambda nil)))) nil)
    eval-last-sexp-1(nil)
    eval-last-sexp(nil)
    call-interactively(eval-last-sexp nil nil)
    command-execute(eval-last-sexp)

Or in the *scratch* buffer:

  (setq lexical-binding t)
  (car (lambda ())) ; => 'closure

[-- Attachment #2: Type: text/html, Size: 867 bytes --]

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

end of thread, other threads:[~2015-01-26 22:22 UTC | newest]

Thread overview: 78+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-21 21:38 [PATCH] Clojure-like syntactic sugar for an anonymous function literal Oleh
2015-01-21 22:28 ` samer
2015-01-21 22:37   ` Oleh
2015-01-21 23:36     ` Artur Malabarba
2015-01-21 23:46       ` Oleh
2015-01-22  0:54         ` Artur Malabarba
2015-01-22  0:57           ` Artur Malabarba
2015-01-22  1:29 ` Daniel Colascione
2015-01-22  2:21   ` Drew Adams
2015-01-22 13:35     ` Lars Brinkhoff
2015-01-22  7:20   ` Stephen J. Turnbull
2015-01-22  8:04     ` Oleh
2015-01-22  8:16       ` David Kastrup
2015-01-22  9:07       ` Andreas Schwab
2015-01-22  9:19         ` Oleh
2015-01-22  8:52   ` René Kyllingstad
2015-01-22  9:17     ` David Kastrup
2015-01-22  9:27       ` Oleh
2015-01-22  9:38         ` Daniel Colascione
2015-01-22  9:45           ` Oleh
2015-01-22  9:50             ` Daniel Colascione
2015-01-22  9:52               ` Oleh
2015-01-22  9:57                 ` Daniel Colascione
2015-01-22 10:05                   ` Oleh
2015-01-22 16:57                     ` Ivan Andrus
2015-01-23  0:54           ` Leo Liu
2015-01-24 23:33           ` Lars Ingebrigtsen
2015-01-22 10:15         ` Stephen J. Turnbull
2015-01-22 10:20           ` David Kastrup
2015-01-22 14:21             ` Stephen J. Turnbull
2015-01-22 14:31               ` Oleh
2015-01-23  1:03                 ` Stephen J. Turnbull
2015-01-22 10:22           ` Oleh
2015-01-22 10:32             ` David Kastrup
2015-01-22 10:40               ` Oleh
2015-01-22 10:56             ` Tassilo Horn
2015-01-22 11:03               ` Oleh
2015-01-22 14:35             ` Stephen J. Turnbull
2015-01-22 14:44               ` Oleh
2015-01-23  1:11                 ` Stephen J. Turnbull
2015-01-22 14:48               ` Artur Malabarba
2015-01-23  1:17                 ` Stephen J. Turnbull
2015-01-22 11:03           ` Phillip Lord
2015-01-22  9:35       ` René Kyllingstad
2015-01-22  9:45         ` Daniel Colascione
2015-01-22  9:49         ` David Kastrup
2015-01-22  9:53           ` Daniel Colascione
2015-01-22 10:22         ` David Kastrup
2015-01-22 12:37   ` Artur Malabarba
2015-01-22 12:46     ` Phillip Lord
2015-01-22 12:49       ` Daniel Colascione
2015-01-22 13:07         ` Oleh
2015-01-22 22:10           ` Richard Stallman
2015-01-23  9:28             ` David Kastrup
2015-01-24  1:09               ` Richard Stallman
2015-01-24  8:29                 ` Thien-Thi Nguyen
2015-01-23 10:33             ` Eli Zaretskii
2015-01-22 16:44 ` Stefan Monnier
2015-01-22 17:22   ` Oleh
2015-01-22 20:34     ` Daniel Colascione
2015-01-22 23:36       ` Stefan Monnier
2015-01-22 23:38         ` Reader macros (Was: Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal) Daniel Colascione
2015-01-23  9:33           ` Reader macros David Kastrup
2015-01-23 11:45             ` Daniel Colascione
2015-01-23 12:27               ` David Kastrup
2015-01-23 10:34         ` [PATCH] Clojure-like syntactic sugar for an anonymous function literal Phillip Lord
2015-01-23 10:47           ` Oleh
2015-01-23 11:53             ` Phillip Lord
2015-01-23 12:02               ` Daniel Colascione
2015-01-23 11:50           ` Daniel Colascione
2015-01-23 13:18             ` Phillip Lord
2015-01-23 20:24           ` Stefan Monnier
2015-01-23 20:52             ` Stefan Monnier
2015-01-23 22:25               ` Phillip Lord
2015-01-23  7:44       ` Oleh
2015-01-22 23:28     ` Stefan Monnier
2015-01-22 18:30   ` Artur Malabarba
  -- strict thread matches above, loose matches on Subject: below --
2015-01-26 22:22 Barry OReilly

Code repositories for project(s) associated with this public inbox

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

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