From 5f408732baf966e820cffdd829c58c08cd7b87c2 Mon Sep 17 00:00:00 2001 From: Nathan Fritzler Date: Wed, 22 Sep 2021 23:28:19 -0400 Subject: [PATCH] add displayln\* from https://github.com/syntax-objects/Summer2021/issues/2 cc @Lazerbeak12345 --- displaylns/displaylns-test.rkt | 11 ++++++++ displaylns/displaylns.rkt | 6 +++++ displaylns/displaylns.scrbl | 46 ++++++++++++++++++++++++++++++++++ index.scrbl | 1 + 4 files changed, 64 insertions(+) create mode 100644 displaylns/displaylns-test.rkt create mode 100644 displaylns/displaylns.rkt create mode 100644 displaylns/displaylns.scrbl diff --git a/displaylns/displaylns-test.rkt b/displaylns/displaylns-test.rkt new file mode 100644 index 0000000..df45950 --- /dev/null +++ b/displaylns/displaylns-test.rkt @@ -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") + +) diff --git a/displaylns/displaylns.rkt b/displaylns/displaylns.rkt new file mode 100644 index 0000000..fba394f --- /dev/null +++ b/displaylns/displaylns.rkt @@ -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 ...)) " "))) diff --git a/displaylns/displaylns.scrbl b/displaylns/displaylns.scrbl new file mode 100644 index 0000000..8a496fa --- /dev/null +++ b/displaylns/displaylns.scrbl @@ -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. + } + ] + +} diff --git a/index.scrbl b/index.scrbl index 6c3a6bd..899b742 100644 --- a/index.scrbl +++ b/index.scrbl @@ -16,6 +16,7 @@ @; ============================================================================= +@include-example{displaylns} @include-example{first-class-or} @include-example{optional-assert} @include-example{make-variable}