[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 6d8abc7 022/316: Add -doto macro (like Clojure's d
From: |
ELPA Syncer |
Subject: |
[elpa] externals/dash 6d8abc7 022/316: Add -doto macro (like Clojure's doto) (#177) |
Date: |
Mon, 15 Feb 2021 15:57:17 -0500 (EST) |
branch: externals/dash
commit 6d8abc7322ab4509f5836c05b8145090d725fb19
Author: Mika Vilpas <mika.vilpas@gmail.com>
Commit: Matus Goljer <dota.keys@gmail.com>
Add -doto macro (like Clojure's doto) (#177)
* Add -doto macro (like Clojure's doto)
Here is its documentation in clojuredocs:
(doto x & forms)
Evaluates x then calls all of the methods and functions with the
value of x supplied at the front of the given arguments. The forms
are evaluated in order. Returns x.
(doto (new java.util.HashMap) (.put "a" 1) (.put "b" 2))
Documentation:
https://clojuredocs.org/clojure.core/doto
Source (clojure):
https://github.com/clojure/clojure/blob/clojure-1.7.0/src/clj/clojure/core.clj#L3700
* Use make-symbol instead of gensym to stay consistent
Consistent with other code in dash.el
* Fix referring to rest from cl.el in -doto
* Add doto example with setcar and setcdr
* Clarify -doto docstring: eval-initial-value evalled only once
Also mention all arguments in the docstring to please checkdoc
* Make -doto documentation more in line with Emacs standards
---
dash.el | 15 +++++++++++++++
dev/examples.el | 6 +++++-
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/dash.el b/dash.el
index 41651a1..8938674 100644
--- a/dash.el
+++ b/dash.el
@@ -71,6 +71,21 @@ special values."
(setq it-index (1+ it-index))
(!cdr ,l)))))
+(defmacro -doto (eval-initial-value &rest forms)
+ "Eval a form, then insert that form as the 2nd argument to other forms.
+The EVAL-INITIAL-VALUE form is evaluated once. Its result is
+passed to FORMS, which are then evaluated sequentially. Returns
+the target form."
+ (declare (indent 1))
+ (let ((retval (make-symbol "value")))
+ `(let ((,retval ,eval-initial-value))
+ ,@(mapcar (lambda (form)
+ (if (sequencep form)
+ `(,(-first-item form) ,retval ,@(cdr form))
+ `(funcall form ,retval)))
+ forms)
+ ,retval)))
+
(defun -each (list fn)
"Call FN with every item in LIST. Return nil, used for side-effects only."
(--each list (funcall fn it)))
diff --git a/dev/examples.el b/dev/examples.el
index 92f8861..ade7eab 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -1020,7 +1020,11 @@ new list."
(defexamples -dotimes
(let (s) (-dotimes 3 (lambda (n) (!cons n s))) s) => '(2 1 0)
- (let (s) (--dotimes 5 (!cons it s)) s) => '(4 3 2 1 0)))
+ (let (s) (--dotimes 5 (!cons it s)) s) => '(4 3 2 1 0))
+
+ (defexamples -doto
+ (-doto '(1 2 3) (!cdr) (!cdr)) => '(3)
+ (-doto '(1 . 2) (setcar 3) (setcdr 4)) => '(3 . 4)))
(def-example-group "Destructive operations" nil
(defexamples !cons
- [elpa] externals/dash 8a46d3c 013/316: [#170] Add a note about -keep on -filter., (continued)
- [elpa] externals/dash 8a46d3c 013/316: [#170] Add a note about -keep on -filter., ELPA Syncer, 2021/02/15
- [elpa] externals/dash 81b15c5 006/316: Fix: The upcoming breaking change is 3.0, not 2.0, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 6484fb5 012/316: Merge pull request #168 from camsaul/patch-2, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 0272e8b 017/316: [Fix #158] -map-last does not modify input list., ELPA Syncer, 2021/02/15
- [elpa] externals/dash 1fce650 019/316: Add `-each-indexed` and an anaphoric equivalent (#178), ELPA Syncer, 2021/02/15
- [elpa] externals/dash 7b2845a 011/316: Fix broken GH profile link in README, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 096f244 018/316: --remove-last does not modify input list #158, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 3e7163a 020/316: [#99] Use butlast to implement -butlast, ELPA Syncer, 2021/02/15
- [elpa] externals/dash eef3bb0 023/316: Document -doto macro in the README (#181), ELPA Syncer, 2021/02/15
- [elpa] externals/dash 7cc0149 021/316: [#99] Add -take-last and -drop-last, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 6d8abc7 022/316: Add -doto macro (like Clojure's doto) (#177),
ELPA Syncer <=
- [elpa] externals/dash f1c60c4 034/316: Merge pull request #193 from GuiltyDolphin/fix-map-when-typo, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 15f384d 025/316: Copyedit: argument name consistency (#187), ELPA Syncer, 2021/02/15
- [elpa] externals/dash 937f8a1 026/316: Update documentation, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 012b3bc 024/316: Docstrings grammar fixes (#186), ELPA Syncer, 2021/02/15
- [elpa] externals/dash 6ded2f2 014/316: Add 'see also' to `-find-index` and `-find-last-index`., ELPA Syncer, 2021/02/15
- [elpa] externals/dash e680ad0 015/316: Merge pull request #173 from Wilfred/master, ELPA Syncer, 2021/02/15
- [elpa] externals/dash a10fb6b 028/316: Add -each-indexed keyword to font-lock, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 502be58 029/316: Make -each-indexed indent like -each, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 4ae329a 032/316: Release 2.13.0, ELPA Syncer, 2021/02/15
- [elpa] externals/dash 0100584 033/316: Correct typo of 'does' to 'do' in -map-when docs, ELPA Syncer, 2021/02/15