Skip to content

Commit

Permalink
add displayln\*
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazerbeak12345 authored and bennn committed Oct 27, 2021
1 parent 50bd615 commit 5f40873
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
11 changes: 11 additions & 0 deletions displaylns/displaylns-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#lang racket/base
(module+ test
(require rackunit racket/port syntax-parse-example/displaylns/displaylns)

(check-equal?
(with-output-to-string
(lambda ()
(displayln* 1 2 3 4 5 '(this is a list of datums 1 2 3 "hi"))))
"1 2 3 4 5 (this is a list of datums 1 2 3 hi)\n")

)
6 changes: 6 additions & 0 deletions displaylns/displaylns.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#lang racket
(provide displayln*)
(require syntax/parse/define)

(define-syntax-parse-rule (displayln* items:expr ...)
(displayln (string-join (map ~a (list items ...)) " ")))
46 changes: 46 additions & 0 deletions displaylns/displaylns.scrbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#lang syntax-parse-example
@require[
(for-label racket/base syntax/parse syntax/parse/define syntax-parse-example/displaylns/displaylns)]

@(define displaylns-eval
(make-base-eval '(require syntax-parse-example/displaylns/displaylns) '(define a-port (current-output-port))))

@title{@tt{displaylns}}

@; =============================================================================

@defmodule[syntax-parse-example/displaylns/displaylns]{}
@stxbee2021["Lazerbeak12345" 2]

@defform[(displayln* expr ...)]{
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
@hyperlink["https://docs.python.org/3/library/functions.html#print" "print"].

To change the output port use @racket[parameterize].

@examples[#:eval displaylns-eval
(displayln* 1 2 3 4 5 '(this is a list of datums 1 2 3 "hi"))
(parameterize ([current-output-port a-port])
(displayln* 1 2 '(a b)))
]

With @racket[define-syntax-parse-rule], this macro is a one-liner:

@racketfile{displaylns.rkt}

A function could express the same behavior.
Furthermore, using a function instead would help reduce code size ---
assuming the compiler does not inline every call to @racket[displayln*].
That said, a macro has two advantages for this debugging tool:
@itemize[
@item{
You can easily to hide all the debug expressions by changing the macro
body to @racket[(void)].
}
@item{
You might extend the macro to compute source locations from its arguments.
}
]

}
1 change: 1 addition & 0 deletions index.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

@; =============================================================================

@include-example{displaylns}
@include-example{first-class-or}
@include-example{optional-assert}
@include-example{make-variable}
Expand Down

0 comments on commit 5f40873

Please sign in to comment.