[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash c3cf98d 438/439: Add `-some->`, `-some->>`, and `-
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash c3cf98d 438/439: Add `-some->`, `-some->>`, and `-some-->` macros. |
Date: |
Tue, 04 Aug 2015 20:31:51 +0000 |
branch: externals/dash
commit c3cf98d692e20be2ba02e198decc22c2f7f5fe84
Author: Cam Saul <address@hidden>
Commit: Matus Goljer <address@hidden>
Add `-some->`, `-some->>`, and `-some-->` macros.
---
README.md | 37 +++++++++++++++++++++++++++++++++++++
dash.el | 27 +++++++++++++++++++++++++++
dev/examples.el | 21 ++++++++++++++++++++-
3 files changed, 84 insertions(+), 1 deletions(-)
diff --git a/README.md b/README.md
index 8fca125..232d6bb 100644
--- a/README.md
+++ b/README.md
@@ -240,6 +240,9 @@ Functions pretending lists are trees.
* [->](#--x-optional-form-rest-more) `(x &optional form &rest more)`
* [->>](#--x-optional-form-rest-more) `(x &optional form &rest more)`
* [-->](#---x-form-rest-more) `(x form &rest more)`
+* [-some->](#-some--x-optional-form-rest-more) `(x &optional form &rest more)`
+* [-some->>](#-some--x-optional-form-rest-more) `(x &optional form &rest more)`
+* [-some-->](#-some---x-optional-form-rest-more) `(x &optional form &rest
more)`
### Binding
@@ -1776,6 +1779,39 @@ in second form, etc.
(--> "def" (concat "abc" it "ghi") upcase) ;; => "ABCDEFGHI"
```
+#### -some-> `(x &optional form &rest more)`
+
+When expr is non-nil, thread it through the first form (via
[`->`](#--x-optional-form-rest-more)),
+and when that result is non-nil, through the next form, etc.
+
+```el
+(-some-> '(2 3 5)) ;; => '(2 3 5)
+(-some-> 5 square) ;; => 25
+(-some-> nil square) ;; => nil
+```
+
+#### -some->> `(x &optional form &rest more)`
+
+When expr is non-nil, thread it through the first form (via
[`->>`](#--x-optional-form-rest-more)),
+and when that result is non-nil, through the next form, etc.
+
+```el
+(-some->> '(1 2 3) (-map 'square)) ;; => '(1 4 9)
+(-some->> '(1 3 5) (-last 'even?) (+ 100)) ;; => nil
+(-some->> '(2 4 6) (-last 'even?) (+ 100)) ;; => 106
+```
+
+#### -some--> `(x &optional form &rest more)`
+
+When expr in non-nil, thread it through the first form (via
[`-->`](#---x-form-rest-more)),
+and when that result is non-nil, through the next form, etc.
+
+```el
+(-some--> "def" (concat "abc" it "ghi")) ;; => "abcdefghi"
+(-some--> nil (concat "abc" it "ghi")) ;; => nil
+(-some--> '(1 3 5) (-filter 'even? it) (append it it) (-map 'square it)) ;; =>
nil
+```
+
## Binding
@@ -2449,6 +2485,7 @@ Change `readme-template.md` or `examples-to-docs.el`
instead.
- [Mark Oteiza](https://github.com/holomorph) contributed the script to
create an info manual.
- [Vasilij Schneidermann](https://github.com/wasamasa) contributed `-some`.
- [William West](https://github.com/occidens) made `-fixfn` more robust at
handling floats.
+ - [Cam SaĆ¼l](https://github.com/cammsaul) contributed `-some->`, `-some->>`,
and `-some-->`.
Thanks!
diff --git a/dash.el b/dash.el
index 772a629..272aee1 100644
--- a/dash.el
+++ b/dash.el
@@ -1199,6 +1199,33 @@ in second form, etc."
(list form x))
`(--> (--> ,x ,form) ,@more)))
+(defmacro -some-> (x &optional form &rest more)
+ "When expr is non-nil, thread it through the first form (via `->'),
+and when that result is non-nil, through the next form, etc."
+ (if (null form) x
+ (let ((result (make-symbol "result")))
+ `(-some-> (-when-let (,result ,x)
+ (-> ,result ,form))
+ ,@more))))
+
+(defmacro -some->> (x &optional form &rest more)
+ "When expr is non-nil, thread it through the first form (via `->>'),
+and when that result is non-nil, through the next form, etc."
+ (if (null form) x
+ (let ((result (make-symbol "result")))
+ `(-some->> (-when-let (,result ,x)
+ (->> ,result ,form))
+ ,@more))))
+
+(defmacro -some--> (x &optional form &rest more)
+ "When expr in non-nil, thread it through the first form (via `-->'),
+and when that result is non-nil, through the next form, etc."
+ (if (null form) x
+ (let ((result (make-symbol "result")))
+ `(-some--> (-when-let (,result ,x)
+ (--> ,result ,form))
+ ,@more))))
+
(defun -grade-up (comparator list)
"Grade elements of LIST using COMPARATOR relation, yielding a
permutation vector such that applying this permutation to LIST
diff --git a/dev/examples.el b/dev/examples.el
index 56d9b5f..ef96ee1 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -733,7 +733,26 @@ new list."
(defexamples -->
(--> "def" (concat "abc" it "ghi")) => "abcdefghi"
(--> "def" (concat "abc" it "ghi") (upcase it)) => "ABCDEFGHI"
- (--> "def" (concat "abc" it "ghi") upcase) => "ABCDEFGHI"))
+ (--> "def" (concat "abc" it "ghi") upcase) => "ABCDEFGHI")
+
+ (defexamples -some->
+ (-some-> '(2 3 5)) => '(2 3 5)
+ (-some-> 5 square) => 25
+ (-some-> nil square) => nil
+ (-some-> 5 even? square) => nil)
+
+ (defexamples -some->>
+ (-some->> '(1 2 3) (-map 'square)) => '(1 4 9)
+ (-some->> '(1 3 5) (-last 'even?) (+ 100)) => nil
+ (-some->> '(2 4 6) (-last 'even?) (+ 100)) => 106
+ (-some->> '("A" "B" :c) (-filter 'stringp) (-reduce 'concat)) => "AB"
+ (-some->> '(:a :b :c) (-filter 'stringp) (-reduce 'concat)) => nil)
+
+ (defexamples -some-->
+ (-some--> "def" (concat "abc" it "ghi")) => "abcdefghi"
+ (-some--> nil (concat "abc" it "ghi")) => nil
+ (-some--> '(1 3 5) (-filter 'even? it) (append it it) (-map 'square it))
=> nil
+ (-some--> '(2 4 6) (-filter 'even? it) (append it it) (-map 'square it))
=> '(4 16 36 4 16 36)))
(def-example-group "Binding"
"Convenient versions of `let` and `let*` constructs combined with flow
control."
- [elpa] externals/dash 63d2db9 431/439: make ->>'s second arg optional, (continued)
- [elpa] externals/dash 63d2db9 431/439: make ->>'s second arg optional, Phillip Lord, 2015/08/04
- [elpa] externals/dash 45bd985 427/439: Reimplementation of --group-by., Phillip Lord, 2015/08/04
- [elpa] externals/dash aa13c07 429/439: fixed typo in docs (first where it should be last, Phillip Lord, 2015/08/04
- [elpa] externals/dash 70caa37 432/439: Merge pull request #144 from cammsaul/thread_last_make_second_arg_optional, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1677338 428/439: Merge pull request #138 from phillord/fix/group-by-write-out, Phillip Lord, 2015/08/04
- [elpa] externals/dash e468937 435/439: Formatting, Phillip Lord, 2015/08/04
- [elpa] externals/dash 89abffe 437/439: Make it explicit that not all VALS are evalled in -if-let*, Phillip Lord, 2015/08/04
- [elpa] externals/dash b88cfb7 436/439: Add dash prefix to epsilon variable., Phillip Lord, 2015/08/04
- [elpa] externals/dash eadb265 439/439: Swap examples to better show how -some-> works, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2aeeacb 433/439: Release 2.11.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash c3cf98d 438/439: Add `-some->`, `-some->>`, and `-some-->` macros.,
Phillip Lord <=
- [elpa] externals/dash 1c2f43d 434/439: Add "See also" for the reduce family, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2b69c14 411/439: Add dash.info and dash.texi to repo, Phillip Lord, 2015/08/04
- [elpa] externals/dash e574465 404/439: Add `-some`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4640a2a 405/439: Allow for approx comparison of floats in tests, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3b63476 412/439: Move anaphoric function intro up into Functions section, Phillip Lord, 2015/08/04
- [elpa] externals/dash 19fbc24 426/439: Fix anaphoric -spice-list macro, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2db56f5 424/439: [-let] Add support for &as bindings (#115), Phillip Lord, 2015/08/04
- [elpa] externals/dash ffa37bf 425/439: Merge pull request #129 from holomorph/docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash 81b808d 409/439: Ignore dash.elc, Phillip Lord, 2015/08/04
- [elpa] externals/dash c61113b 420/439: [-let] Update outdated comment, Phillip Lord, 2015/08/04