Skip to content

Commit 5f40873

Browse files
Lazerbeak12345bennn
authored andcommitted
1 parent 50bd615 commit 5f40873

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

displaylns/displaylns-test.rkt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#lang racket/base
2+
(module+ test
3+
(require rackunit racket/port syntax-parse-example/displaylns/displaylns)
4+
5+
(check-equal?
6+
(with-output-to-string
7+
(lambda ()
8+
(displayln* 1 2 3 4 5 '(this is a list of datums 1 2 3 "hi"))))
9+
"1 2 3 4 5 (this is a list of datums 1 2 3 hi)\n")
10+
11+
)

displaylns/displaylns.rkt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#lang racket
2+
(provide displayln*)
3+
(require syntax/parse/define)
4+
5+
(define-syntax-parse-rule (displayln* items:expr ...)
6+
(displayln (string-join (map ~a (list items ...)) " ")))

displaylns/displaylns.scrbl

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#lang syntax-parse-example
2+
@require[
3+
(for-label racket/base syntax/parse syntax/parse/define syntax-parse-example/displaylns/displaylns)]
4+
5+
@(define displaylns-eval
6+
(make-base-eval '(require syntax-parse-example/displaylns/displaylns) '(define a-port (current-output-port))))
7+
8+
@title{@tt{displaylns}}
9+
10+
@; =============================================================================
11+
12+
@defmodule[syntax-parse-example/displaylns/displaylns]{}
13+
@stxbee2021["Lazerbeak12345" 2]
14+
15+
@defform[(displayln* expr ...)]{
16+
This macro is intended to make debugging easier by allowing a programmer to
17+
print a batch of values all in one go much like Python's
18+
@hyperlink["https://docs.python.org/3/library/functions.html#print" "print"].
19+
20+
To change the output port use @racket[parameterize].
21+
22+
@examples[#:eval displaylns-eval
23+
(displayln* 1 2 3 4 5 '(this is a list of datums 1 2 3 "hi"))
24+
(parameterize ([current-output-port a-port])
25+
(displayln* 1 2 '(a b)))
26+
]
27+
28+
With @racket[define-syntax-parse-rule], this macro is a one-liner:
29+
30+
@racketfile{displaylns.rkt}
31+
32+
A function could express the same behavior.
33+
Furthermore, using a function instead would help reduce code size ---
34+
assuming the compiler does not inline every call to @racket[displayln*].
35+
That said, a macro has two advantages for this debugging tool:
36+
@itemize[
37+
@item{
38+
You can easily to hide all the debug expressions by changing the macro
39+
body to @racket[(void)].
40+
}
41+
@item{
42+
You might extend the macro to compute source locations from its arguments.
43+
}
44+
]
45+
46+
}

index.scrbl

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
@; =============================================================================
1818

19+
@include-example{displaylns}
1920
@include-example{first-class-or}
2021
@include-example{optional-assert}
2122
@include-example{make-variable}

0 commit comments

Comments
 (0)