Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure functions for Emacs?

Tags:

emacs

clojure

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.

like image 984
Eli Schneider Avatar asked Oct 08 '10 02:10

Eli Schneider


People also ask

What is cider Clojure?

CIDER is the Clojure(Script) Interactive Development Environment that Rocks! CIDER extends Emacs with support for interactive programming in Clojure. The features are centered around cider-mode, an Emacs minor-mode that complements clojure-mode.


Video Answer


2 Answers

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)))
like image 120
Michał Marczyk Avatar answered Oct 11 '22 14:10

Michał Marczyk


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.

like image 30
Bozhidar Batsov Avatar answered Oct 11 '22 13:10

Bozhidar Batsov