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

displayln* - a macro with similar use to python's print #2

Open
Lazerbeak12345 opened this issue Jul 3, 2021 · 3 comments
Open

displayln* - a macro with similar use to python's print #2

Lazerbeak12345 opened this issue Jul 3, 2021 · 3 comments

Comments

@Lazerbeak12345
Copy link

Lazerbeak12345 commented Jul 3, 2021

Please enter the bee by submitting code (or links to code) for:

  1. your macro
  2. an example use of your macro
  3. (optional) "before" code that your macro helps to improve

Thank you for your submission!

If your entry is a PR to the syntax parse examples repository, please include a link to the PR.

Macro

Paste the code for your macro here.
Please explain the purpose of the macro.

#lang racket
(require syntax/parse/define)
#|This macro is intended to make debugging easier by allowing a programmer to
print a batch of values all in one go much like python's `print`

To change the output port use `parameterize`.|#
(define-syntax-parse-rule (displayln* items:expr ...)
  (println (string-join (map ~a (list items ...)) " ")))

I can easily rewrite this without using the define-syntax-parse-rule macro (which expands down to a call to syntax-parse and a bit of boilerplate), but I chose this approach because of how readable (and thus maintainable) the result is if this were used in real code.

Example

Illustrate one or more ways of using your macro.
Please show code and briefly describe what it does.

(displayln* 1 2 3 4 5 '(this is a list of datums 1 2 3 "hi"))
; should print "1 2 3 4 5 '(this is a list of datums 1 2 3 \"hi\")\n" exactly

Before and After

If you designed your macro to improve some existing code, please explain the improvements.

Use the following categories if applicable:

  • Code Cleaning : Please share the code that you used to write before creating your macro. Briefly explain how the code works.
  • Macro Engineering : Please share the old macro that you revised. Briefly explain the changes.

Before I would have used formats like this:

(println (format "~a ~a ~a" 1 2 '(a b)))

Now the same code can be written like this:

(displayln* 1 2 '(a b))

One downside is that specifying the port can't be done with the last argument anymore, but the simple workaround for that case is simply using parameterize on (current-output-port):

before

(println (format "~a ~a ~a" 1 2 '(a b)) the-port)

after:

(parameterize ([current-output-port the-port])
   (displayln* 1 2 '(a b)))

Licence

Please confirm that you are submitting this code under the same MIT License that the Racket language uses. https://github.com/racket/racket/blob/master/racket/src/LICENSE-MIT.txt
Please confirm that the associated text is licensed under the Creative Commons Attribution 4.0 International License http://creativecommons.org/licenses/by/4.0/

I confirm that the code is under the same license as the Racket language, and associated text is under Creative Commons Attribution 4.0 International License

Contact

To receive prizes and/or provide feedback please complete
the form at https://forms.gle/Z5CN2xzK13dfkBnF7 (google account not required / email optional).

The google form didn't work...

@bennn
Copy link
Member

bennn commented Jul 5, 2021

Thank you for entering!!

Please try the form again. I clicked a button to have it start collecting entries. https://forms.gle/Z5CN2xzK13dfkBnF7
(No idea why it stopped in the first place)

@Lazerbeak12345
Copy link
Author

Reading over this again, this looks doable with just a define form - no macros. I'm... not familiar enough with define to say for sure though.

@bennn
Copy link
Member

bennn commented Sep 20, 2021

Yep that's true. Here's the define syntax:

(define (displayln* . items)
  (println (string-join (map ~a items) " ")))

But still, it's nice to have simple useful examples to see how things like ... work.

bennn added a commit to syntax-objects/syntax-parse-example that referenced this issue Sep 23, 2021
bennn pushed a commit to syntax-objects/syntax-parse-example that referenced this issue Oct 27, 2021
bennn pushed a commit to syntax-objects/syntax-parse-example that referenced this issue Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants