emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Clojure-like syntactic sugar for an anonymous function liter


From: Oleh
Subject: Re: [PATCH] Clojure-like syntactic sugar for an anonymous function literal
Date: Thu, 22 Jan 2015 11:40:40 +0100

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

If it were up to me, I'd use `cl-loop' everywhere. But I'm not in a
vacuum: I use and monitor many packages. And I can't just tell 60
random people: "Dont use `dash', that approach is silly, use `cl-loop'
instead". What I can do though, is to provide an alternative for core
Emacs with no disadvantages of the anaphoric macros of `dash'.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]