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 wrap clause #68

Closed

Conversation

Luis-Henriquez-Perez
Copy link
Contributor

@Luis-Henriquez-Perez Luis-Henriquez-Perez commented Apr 18, 2021

This is a PR to add the wrap clause. The idea is that it would wrap the loopy body with forms that "wrap" a body. The goal would be to reduce nesting.

(loopy (wrap (with-current-buffer "*Messages*") (save-excursion) (save-match-data))
       (while (re-search-forward "hello" nil t nil))
       (collect (match-string-no-properties 0)))

;; =>
(with-current-buffer "*Messages*"
  (save-excursion
    (save-match-data
      (loopy (while (re-search-forward "hello" nil t nil))
	     (collect (match-string-no-properties 0)))

I am a bit unsure as to the implementation of this. I believe this is related to the questions I had in #67. I looked at commands which wrap others such as if when unless. Unlike those commands the wrap command does not take a body. But I still need access to the body of the loop so I can wrap it.

I'm looking towards with because it actually wraps the body the way I want to. However with is not implemented in loopy-commands.el with the other commands. Perhaps this is getting at a root question. Is it possible to implement with as a loopy command? I think that ideally we'd want this to be possible.

@okamsn
Copy link
Owner

okamsn commented Apr 18, 2021

I'm looking towards with because it actually wraps the body the way I want to. However with is not implemented in loopy-commands.el with the other commands. Perhaps this is getting at a root question. Is it possible to implement with as a loopy command? I think that ideally we'd want this to be possible.

Right now, it is technically possible but only superficially different from how it currently works.

The function loopy--expand-to-body is written to build the expanded code based on what that expanded code can look like.

loopy/loopy.el

Lines 760 to 764 in 7d2d52f

;; Declare the With variables.
(when loopy--with-vars
(setq result `(,@(loopy--destructure-for-with-vars loopy--with-vars)
,@(get-result))
result-is-one-expression t))

https://github.com/okamsn/loopy/blob/7d2d52f1b7191fe795adf0ee0adc5551e99d75f0/loopy.el#L593-#L622

To add a way to wrap the body of the loop, you would need to add code to loopy--expand-to-body. You would also probably want to create a variable like loopy--wrapping-forms and add it to loopy--variables.

In my mind, the difference between loop commands and the special macro arguments is that special macro arguments handle code outside of the loop and the loop commands handle code inside of the loop (and set-up some code to make sure the loop works).

Each way to add code outside of the loop is a special case, which is why they each have a special macro argument. It is easy, on the other hand, to enclose any number of expressions inside the while loop, allowing for the implementation of loop commands to be more generic.

@okamsn okamsn added the enhancement New feature or request label May 8, 2021
@okamsn
Copy link
Owner

okamsn commented Jun 10, 2021

A made a version of this using a new special macro argument in #76. I will try to merge that version in a few days if I find nothing wrong with it.

I apologize for taking so long to get to this. I don't have as much free time as I once did.

Please comment with any concerns in that new PR. Thank you.

@okamsn okamsn closed this Jun 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants