简体   繁体   English

Emacs的Clojure功能?

[英]Clojure functions for Emacs?

I wondering if there is a set of Emacs Lisp code that implements some of Clojure's functions. 我想知道是否有一组Emacs Lisp代码实现了一些Clojure的功能。 For example, -> and ->> and comp and partial, and others? 例如, - >和 - >>和comp和partial,以及其他?

Thank you. 谢谢。

I've ported the -> and ->> macros to Emacs Lisp a while ago. 我刚才将->->>宏移植到了Emacs Lisp。 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 . 你一定要看看dash.el。 It provides a lot of Clojure-inspired functions like: 它提供了许多Clojure灵感的功能,如:

  • -map (fn list) -map (fn list)
  • -reduce-from (fn initial-value list) -reduce-from (fn initial-value list)
  • -reduce-r-from (fn initial-value list) -reduce-r-from (fn initial-value list)
  • -reduce (fn list) -reduce (fn list)
  • -reduce-r (fn list) -reduce-r (fn list)
  • -filter (pred list) -filter (pred list)
  • -remove (pred list) - 删除(pred list)
  • -keep (fn list) -keep (fn list)
  • -map-when (pred rep list) -map-when (pred rep list)
  • -map-indexed (fn list) -map-indexed (fn list)
  • -flatten (l) -flatten (l)
  • -concat (&rest lists) -concat (&rest lists)
  • -mapcat (fn list) -mapcat (fn list)
  • -cons* (&rest args) -cons * (&rest args)
  • -count (pred list) -count (pred list)
  • -any? -任何? (pred list)
  • -all? -所有? (pred list)
  • -none? -没有? (pred list)
  • -only-some? -只有一些? (pred list)
  • -each (list fn) -each (list fn)
  • -each-while (list pred fn) -each-while (list pred fn)
  • -dotimes (num fn) -dotimes (num fn)
  • -repeat (nx) -repeat (nx)
  • -slice (list from &optional to) -slice (list from &optional to)
  • -take (n list) -take (n list)
  • -drop (n list) -drop (n list)
  • -take-while (pred list) -take-while (pred list)
  • -drop-while (pred list) -drop-while (pred list)
  • -split-at (n list) -split-at (n list)
  • -insert-at (nx list) -insert-at (nx list)
  • -split-with (pred list) -split-with (pred list)
  • -separate (pred list) - 分离(pred list)
  • -partition (n list) - 分区(n list)
  • -partition-all-in-steps (n step list) -partition-all-in-steps (n step list)
  • -partition-in-steps (n step list) - 分步(n step list)
  • -partition-all (n list) -partition-all (n list)
  • -partition-by (fn list) -partition-by (fn list)
  • -partition-by-header (fn list) -partition-by-header (fn list)
  • -group-by (fn list) -group-by (fn list)
  • -interpose (sep list) -interpose (sep list)
  • -interleave (&rest lists) -interleave (&rest lists)
  • -zip-with (fn list1 list2) -zip-with (fn list1 list2)
  • -zip (list1 list2) -zip (list1 list2)
  • -first (pred list) - 第一个(pred list)
  • -last (pred list) -last (pred list)
  • -union (list list2) -union (list list2)
  • -difference (list list2) -difference (list list2)
  • -intersection (list list2) -intersection (list list2)
  • -distinct (list) -distinct (list)
  • -contains? -contains? (list element)
  • -sort (predicate list) -sort (predicate list)
  • -partial (fn &rest args) -partial (fn &rest args)
  • -rpartial (fn &rest args) -rpartial (fn &rest args)
  • -applify (fn) -applify (fn)
  • -> (x &optional form &rest more) - > (x &optional form &rest more)
  • ->> (x form &rest more) - >> (x form &rest more)
  • --> (x form &rest more) - > (x form &rest more)
  • -when-let (var-val &rest body) -when-let (var-val &rest body)
  • -when-let* (vars-vals &rest body) -when-let * (vars-vals &rest body)
  • -if-let (var-val then &optional else) -if-let (var-val then &optional else)
  • -if-let* (vars-vals then &optional else) -if-let * (vars-vals then &optional else)
  • !cons (car cdr) !cons (car cdr)
  • !cdr (list) !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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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