【发布时间】:2017-03-21 16:47:14
【问题描述】:
把这张地图打印出来很丑:
{:type :move,
:name :boost,
:from
{:nodeid :plus,
:name :left-operand,
:value
{:args [:result :right-operand],
:f
#object[fargish.workspace_test$fn__159675$fn__159678 0xb3f518f "fargish.workspace_test$fn__159675$fn__159678@b3f518f"]},
:dockclass :input,
:ref [:plus :left-operand]},
:to
{:nodeid :source11,
:name :output,
:value 11,
:dockclass :output,
:ref [:source11 :output]},
:do
#object[fargish.workspace$do_boost 0x179d226e "fargish.workspace$do_boost@179d226e"],
:do-hypothetically
#object[fargish.workspace$do_hypothetical_boost 0x68d6475a "fargish.workspace$do_hypothetical_boost@68d6475a"]}
大多数时候,我不需要看到大部分内容。我希望它看起来大致像这样:
#boost{:from [:plus :left-operand] :to [:source11 :output]}
Clojure 是否提供了一个钩子让我插入代码,以便如果映射的:type 键的值为:move,str 将调用我编写的函数来生成字符串?
我已经调查过print-method。如果我理解正确,那将扩展一个多方法,该方法在参数类型上调度,即。 clojure.lang.PersistentArrayMap,而不是地图的:type。 AFAIK,没有办法“转发”到先前定义的多方法,否则我可以为 clojure.lang.PersistentArrayMap 写一个print-method,它查看:type,然后调用泛型方法,如果它的值不是@987654333 @。
即使只是自定义 pprint 也会有所帮助。从理论上讲,pprint 是非常可定制的,但我还没有找到 documentation 来解释如何。 This 不完整,表明 pprint 还没有真正完成。
影响pprint 的输出会很好,但这并不是绝对必要的。只要能够影响str 和print 就会有很大帮助。
【问题讨论】:
标签: clojure string-formatting pretty-print