Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ->>as-> function for use with ->> #411

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this file is auto-generated from the docstrings in dash.el and the first three examples in dev/examples.el.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So i'd have to rewrite the tests in order for it to work?
I didn't notice that when I was writing sorry.

Also I forgot to email FSF because other things cropped up -- shall have to ask about anonymous assignments + maybe PO boxes.

Copy link
Collaborator

@basil-conto basil-conto Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So i'd have to rewrite the tests in order for it to work?

The first three tests in dev/examples.el should be self-contained and representative, then make docs regenerates both README.md and dash.texi accordingly.

If you have any difficulties or would rather avoid running make docs on your end, that's no problem, one of the maintainers or I can do so on the PR branch before merging.

I didn't notice that when I was writing sorry.

No problem at all.

Also I forgot to email FSF because other things cropped up -- shall have to ask about anonymous assignments + maybe PO boxes.

Good luck! Keep us posted.

Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ or readability.
* [`->>`](#--x-optional-form-rest-more) `(x &optional form &rest more)`
* [`-->`](#---x-rest-forms) `(x &rest forms)`
* [`-as->`](#-as--value-variable-rest-forms) `(value variable &rest forms)`
* [`->>as->`](#->>as->variable-rest-forms-value) `(value forms ... value)`
* [`-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---expr-rest-forms) `(expr &rest forms)`
Expand Down Expand Up @@ -2528,6 +2529,17 @@ In the first form, bind `variable` to `value`. In the second form, bind
(-as-> 3 my-var) ;; => 3
```

#### ->>as-> (variable forms ... value)
Ending with VALUE, thread VARIABLE through all the other FORMS.
Variant of `-as->` but intended for use with `->>`.

```el
(->>as-> i (+ i 1) 1) ;; => 2
;; ^ unintuitive on its own.
(->> 1 (->>as-> i (+ i 1))) ;; => 2
(->> "def" (->>as-> string (concat "abc" string "ghi"))) ;; => "abcdefghi"
```

#### -some-> `(x &optional form &rest more)`

When expr is non-`nil`, thread it through the first form (via [`->`](#--x-optional-form-rest-more)),
Expand Down
13 changes: 13 additions & 0 deletions dash.el
Original file line number Diff line number Diff line change
Expand Up @@ -2127,6 +2127,19 @@ VARIABLE to the result of the first form, and so forth."
,variable
,@(cdr forms)))))

(defmacro ->>as-> (variable &rest forms)
"Ending with VALUE, thread VARIABLE through all the other FORMS.
Variant of `-as->' but intended for use with `->>'.

\(fn VARIABLE FORMS ... VALUE)"
(if (null forms)
(error "No Value nor Forms given!")
(let ((val (car (last forms)))
(args (-butlast forms)))
(if (null args)
`,val
`(-as-> ,val ,variable ,@args)))))

(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."
Expand Down
8 changes: 8 additions & 0 deletions dev/examples.el
Original file line number Diff line number Diff line change
Expand Up @@ -2064,6 +2064,14 @@ or readability."
(-as-> "def" string (concat "abc" string "ghi") upcase) => "ABCDEFGHI"
(-as-> "def" string (concat "abc" string "ghi") (upcase string)) => "ABCDEFGHI")

(defexamples ->>as->
(->>as-> i 1) => 1
(->>as-> i (+ i 1) 1) => 2
(->> 1 (->>as-> i (+ i 1))) => 2
(->> "def" (->>as-> string (concat "abc" string "ghi"))) => "abcdefghi"
(->> "def" (->>as-> string (concat "abc" string "ghi")) upcase) => "ABCDEFGHI"
(->> "def" (->>as-> string (concat "abc" string "ghi") (upcase string))) => "ABCDEFGHI")

(defexamples -some->
(-some-> '(2 3 5)) => '(2 3 5)
(-some-> 5 square) => 25
Expand Down