[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] Clojure-like syntactic sugar for an anonymous function liter
From: |
David Kastrup |
Subject: |
Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal |
Date: |
Thu, 22 Jan 2015 11:32:56 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) |
Oleh <address@hidden> writes:
>> > The most popular library in MELPA, https://github.com/magnars/dash.el,
>> > implements it (for a long time) like this:
>> >
>> > (--map (* it it) '(1 2 3))
>> > ;; => (1 4 9)
>> >
>> > With my approach, it's:
>> >
>> > (mapcar #(* % %) '(1 2 3))
>> > ;; => (1 4 9)
>>
>> That looks almost like Perl! Now I'm -2. Just require dash.
>
> How is `dash' better? `--map' is a macro:
>
> (defmacro --map (form list)
> "Anaphoric form of `-map'."
> (declare (debug (form form)))
> `(mapcar (lambda (it) ,form) ,list))
>
> `dash' also gives other ~40 macros that look like this, littered all
> over the code in the MELPA, so it's impossible to go on without
> understanding what `dash' does.
>
> On the other hand, `mapcar' is a C function. It and all other
> functions can use `short-lambda' instead of being reimplemented as
> macros on a case-per-case basis by `dash'.
So use cl-loop. Has the advantage of being _both_ concise as well as
efficient after compilation since Emacs Lisp is not really fast at
function calls.
(cl-loop for i from 1 to 3 collect (* i i))
Or (cl-loop for i in '(1 2 3) collect (* i i))
The code cl-loop creates is usually quite faster than any of the map*
functions. I haven't checked with lexical bindings though: it is
conceivable that the anonymous lambda cost goes down for them, but so
does the variable-binding cost for cl-loop.
--
David Kastrup
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, (continued)
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Leo Liu, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Stephen J. Turnbull, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, David Kastrup, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Stephen J. Turnbull, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Oleh, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Stephen J. Turnbull, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Oleh, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal,
David Kastrup <=
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Oleh, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Tassilo Horn, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Oleh, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Stephen J. Turnbull, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Oleh, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Stephen J. Turnbull, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Artur Malabarba, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Stephen J. Turnbull, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, Phillip Lord, 2015/01/22
- Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal, René Kyllingstad, 2015/01/22