简体   繁体   中英

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.

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 (nx)
  • -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 (nx 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.

不确定其他人,但部分是在Emacs的lexbind分支中实现为“咖喱”。

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM