简体   繁体   English

将System.out.format转换为Clojure

[英]Translate System.out.format to Clojure

I have written some utility programs in Java, and now I just want to translate it into Clojure . 我用Java编写了一些实用程序,现在我只想把它翻译成Clojure I am running into some snags with classes. 我遇到了一些课程障碍。 Clojure touts that it has seamless interoperation with Java, but I have not been able to find good solution from Google. Clojure宣称它与Java无缝互操作,但我无法从谷歌找到好的解决方案。 Please help. 请帮忙。 Thanks. 谢谢。

I want to use Java classes directly (I don't want to use clojure "format" function yet because I just want to see how the clojure-java-interop works out): 我想直接使用Java类(我不想使用clojure“format”函数,因为我只想看看clojure-java-interop是如何工作的):

System.out.format("Enter number of points: ");

What I have done is: 我所做的是:

(def x (. System out))

but then everything I tried to do to use format failed: 但是我尝试使用格式的所有内容都失败了:

(. x format "foo")
(. x (format "foo"))
(.format x)
(.format "foo")
(. x format)
(. x #(format))
(. x #(format %) "s")
(.format x "foo")
((.format x) "foo")
(x/format "foo")
(x. format "%s" "foo")
(. x format "%s" "s")
(. x format "%s" ["s"])
(def y (System.out.))
(def y (System.out.format.))
(format x "s")

And what about translating System.exit(0) to clojure? 那么将System.exit(0)翻译成clojure呢?

(. System exit 0) 

does seem to work. 确实有效。 But why doesn't similar translation work for "System.out.format"? 但为什么类似的翻译不适用于“System.out.format”?

I seem like a monkey typing on the key board hoping to produce Hamlet! 我好像在键盘上打字,希望能产生哈姆雷特!

Please help ! 请帮忙 ! Thanks. 谢谢。

System.out.format takes in variable arguments. System.out.format接受变量参数。 The way java dispatches var args functions is by shoving the rest of the arguments into an Object array. java调度var args函数的方法是将其余参数推送到Object数组中。 This could be achieved in clojure like so: 这可以在clojure中实现,如下所示:

(. System/out format "abc" (into-array []))
(. System/out format "abc %d" (into-array [12]))

;; or use the more intuitive
(.format System/out "abc %d" (into-array[12]))

actually lots of your attempts were really very close: 实际上你的很多尝试都非常接近:

(def x (. System out))
(. x format "foo" (into-array[]))
(. x (format "foo" (into-array[])))
(.format x "foo" (into-array[]))
(. x format "%s" (into-array["foo"]))

However, pay attention, this will print out to the repl console and not necessarily to what your ide shows. 但是,要注意,这将打印到repl控制台,而不一定是你的ide显示。

To show it like clojure would, instead of using the java's System.out object, use clojure's *out* : 要像clojure一样显示它,而不是使用java的System.out对象,使用clojure的*out*

(. *out* format "abc %d" (into-array [12])) 
;; "abc 12"

EDIT 编辑

It seems that your *out* is defined as a OutputStreamWriter which does not have the method format . 看来你的*out*被定义为没有方法formatOutputStreamWriter Not sure why, but you could overcome this using binding, for example: 不知道为什么,但你可以使用绑定来克服这个问题,例如:

user=> (binding [*out* System/out]
         (. *out* format "abc %d" (into-array[12])))
abc 12#object[java.io.PrintStream 0x4efb0c88 "java.io.PrintStream@4efb0c88"]

Clojure already has a printf function that does the same thing as PrintStream.format , except that it prints specifically to *out* : Clojure已经有一个printf函数与PrintStream.format做同样的事情,除了它专门打印到*out*

(printf "Enter number of points: ")
;; Enter number of points: 

(printf "abc %d" 12)
;; abc 12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM