prettyfied repl

Hy 0.13.0 introduced a new command line switch to repl: –repl-output-fn, but what is it used for?

In a short, that command line switch is used to post processing of REPL output. There’s an example implementation that is shipped with Hy, but nothing prevents user from rolling on their own. To test this feature, try it as follows:

$ hy --repl-output-fn hy.contrib.hy-repr.hy-repr
hy 0.13.0 using CPython(default) 3.4.3 on Linux
=> '(+ 1 1)
'(+ 1 1)

Result of quoted code is exactly as user typed it. Without the switch, following happens:

=> '(+ 1 1)
('+' 1 1)

While it might seem like a minor thing, this is huge help when working with complicated macros and examining the output with macroexpand (I added some linebreaks to make it more readable):

=> (macroexpand '(cond [(= a 1) (print "it was one")]
...                     [(= a 2) (print "it was two")]
...                     [(print "I can't cound that far")]))
('if*' ('=' 'a' 1) ('do' ('print' 'it was one'))
       ('if' ('if' ('=' 'a' 2) ('do' ('print' 'it was two'))
             ('if' ('do' ('setv' ':G_1235' ('print' "I can't cound that far"))


=> (macroexpand '(cond [(= a 1) (print "it was one")]
...                     [(= a 2) (print "it was two")]
...                     [(print "I can't cound that far")]))
'(if* (= a 1) (do (print "it was one"))
      (if (if (= a 2) (do (print "it was two"))
          (if (do (setv :G_1235 (print "I can't cound that far"))

The goal is to use representation that is as close as possible to what user actually typed. This way it’s easier to keep track what’s going on and also copy / paste things around and evaluate them again and again. Compare the ubiquitous tupple:

=> (, 1 2 3)
(1, 2, 3)

versus prettyfied

=> (, 1 2 3)
(, 1 2 3)

The latter one can be easily copied and used as is as part of program. Really handy and much more pleasing to eye too.

The implementation of hy-repr be interesting to investigate further. Essentially it’s a case study which recursively processes an object and constructs a prettyfied string representation of it. As the code is in GitHub, users can dissect it and write their own if such a need arises.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s