在Clojure中按:type定制地图打印 [英] Customizing the printing of maps by :type in Clojure

查看:42
本文介绍了在Clojure中按:type定制地图打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打印这张地图很丑陋:

  {: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"]}

大多数时候,我不需要看大部分.我希望它看起来像这样:

Most of the time, I don't need to see most of that. I'd like it to look something roughly like this:

#boost{:from [:plus :left-operand] :to [:source11 :output]}

Clojure是否提供了一个挂钩让我插入代码,以便如果地图的:type 键的值是:move str 会调用我编写的函数来生成字符串吗?

Does Clojure provide a hook to let me insert code so that if the value of a map's :type key is :move, str will call a function I write to generate the string?

我研究了 打印方法 .如果我正确理解的话,那将扩展派发自变量类型viz的多重方法.clojure.lang.PersistentArrayMap,而不是地图的:type .AFAIK,无法转发"到先前定义的多重方法,否则我可以为clojure.lang.PersistentArrayMap编写一个 print-method ,并查看:type 和如果其值不是:move ,则调用通用方法.

I've looked into print-method. If I understand it correctly, that would extend a multimethod that dispatches on the type of the argument, viz. clojure.lang.PersistentArrayMap, not the :type of the map. AFAIK, there's no way to "forward" to the previously defined multimethod, or else I could write a print-method for clojure.lang.PersistentArrayMap that looked at :type and then called the generic method if its value isn't :move.

即使只是自定义 pprint 也将有所帮助.从理论上讲, pprint 是非常可定制的,但是我还没有找到不完整,表明 pprint 尚未真正完成.

Even just customizing pprint would be helpful. Theoretically, pprint is very customizable, but I haven't found documentation that explains how. This is incomplete and suggests that pprint isn't really done.

影响 pprint 的输出会很好,但并非绝对必要.仅仅能够影响 str print 将会是一个很大的帮助.

Affecting the output of pprint would be nice but it isn't strictly necessary. Just being able to affect str and print would be a great help.

推荐答案

打印方法

print-method already looks at the :type key on the metadata of your object. Just arrange for :type to be in the metadata rather than (or in addition to) the actual map itself, and you can define your own print-method.

这篇关于在Clojure中按:type定制地图打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆