Clojure functions for Emacs?
I wondering if there is a set of Emacs Lisp code that implements some of Clojure's functions. For example, -> and ->> and comp and partial, and others?
Thank you.开发者_StackOverflow中文版
I've ported the ->
and ->>
macros to Emacs Lisp a while ago. I use them occasionally in my configuration code and they seem to work fine.
(defmacro -> (e &rest es)
(if (and (consp es) (not (consp (cdr es))))
(if (consp (car es))
`(,(caar es) ,e ,@(cdar es))
`(,(car es) ,e))
(if (consp es)
`(-> (-> ,e ,(car es)) ,@(cdr es))
e)))
(defmacro ->> (e &rest es)
(if (and (consp es) (not (consp (cdr es))))
(if (consp (car es))
`(,@(car es) ,e)
`(,(car es) ,e))
(if (consp es)
`(->> (->> ,e ,(car es)) ,@(cdr es))
e)))
You should definitely take a look at dash.el. It provides a lot of Clojure-inspired functions like:
- -map
(fn list)
- -reduce-from
(fn initial-value list)
- -reduce-r-from
(fn initial-value list)
- -reduce
(fn list)
- -reduce-r
(fn list)
- -filter
(pred list)
- -remove
(pred list)
- -keep
(fn list)
- -map-when
(pred rep list)
- -map-indexed
(fn list)
- -flatten
(l)
- -concat
(&rest lists)
- -mapcat
(fn list)
- -cons*
(&rest args)
- -count
(pred list)
- -any?
(pred list)
- -all?
(pred list)
- -none?
(pred list)
- -only-some?
(pred list)
- -each
(list fn)
- -each-while
(list pred fn)
- -dotimes
(num fn)
- -repeat
(n x)
- -slice
(list from &optional to)
- -take
(n list)
- -drop
(n list)
- -take-while
(pred list)
- -drop-while
(pred list)
- -split-at
(n list)
- -insert-at
(n x list)
- -split-with
(pred list)
- -separate
(pred list)
- -partition
(n list)
- -partition-all-in-steps
(n step list)
- -partition-in-steps
(n step list)
- -partition-all
(n list)
- -partition-by
(fn list)
- -partition-by-header
(fn list)
- -group-by
(fn list)
- -interpose
(sep list)
- -interleave
(&rest lists)
- -zip-with
(fn list1 list2)
- -zip
(list1 list2)
- -first
(pred list)
- -last
(pred list)
- -union
(list list2)
- -difference
(list list2)
- -intersection
(list list2)
- -distinct
(list)
- -contains?
(list element)
- -sort
(predicate list)
- -partial
(fn &rest args)
- -rpartial
(fn &rest args)
- -applify
(fn)
- ->
(x &optional form &rest more)
- ->>
(x form &rest more)
- -->
(x form &rest more)
- -when-let
(var-val &rest body)
- -when-let*
(vars-vals &rest body)
- -if-let
(var-val then &optional else)
- -if-let*
(vars-vals then &optional else)
- !cons
(car cdr)
- !cdr
(list)
I find this library extremely useful.
Not sure about the others, but partial is implemented in the lexbind branch of Emacs as "curry".
I've written these macros recently. They're not recursive and are less verbose. But I haven't tested them extensively yet.
(defmacro ->> (x &rest forms)
(while forms
(setq x (append (pop forms) (list x))))
x)
(defmacro -> (x &rest forms)
(while forms
(let ((form (pop forms)))
(push x (cdr form))
(setq x form)))
x)
精彩评论