在 Clojure 中:在函数内部使用 let 执行 Java 调用时出错,但在 REPL 中则不然

发布于 2024-09-08 19:31:57 字数 2562 浏览 10 评论 0原文

我有以下代码:

(defn post
  [title content timestamp]
    (let [[innholdet tajm]
       [(str "<html>
              <head>
              <title>" title " :: " blog_title "</title></head>
              <body><h1>" title "</h1>
              <br/>" content "<br/><i>posted " (Date. timestamp) "</i>
              <br/><a href=\"" (str blog_url) "\">[main]</a> | 
              <a href=\"" (str blog_url) "/" timestamp ".html\">[permalink]</a>
              </body></html>") (str timestamp".html")]]
                 (spit tajm innholdet)
   )
 )

我有另一个函数,它将标题、内容和 UNIX 时间戳传递给上面的函数“post”。 blog_title 和 blog_url 是在代码顶部定义的 var。我已经对 HTML 进行了美观排序,但在“真实代码”中情况并非如此。我也在做

(import 'java.util.Date)
(use 'clojure.string)

如果我尝试执行代码,我会收到错误

java.lang.IllegalArgumentException 

如果我删除 (Date.timestamp) 代码可以完美执行,但我需要具有 (Date.) 函数。

现在,如果在 REPL 中我这样做

(import 'java.util.Date)

然后定义一个带有时间戳的 var

(def timestamp 1278854531000) ;; Just for pushing a var into (Date.) than just the number

然后我只需将 (let) 复制到 REPL 中并调整原始代码中的变量以表示不同的函数名称而不是值(因为这是REPL 和原始代码中的变量、函数和值不存在),就像这样:

(let [[innholdet tajm] 
  [(str "<html>
         <head>
         <title>" (str "title") " :: " (str "blog_title") "</title>
         </head>
         <body><h1>" (str "title") "</h1><br/>" (str "content") "<br/>
         <i>posted " (Date. timestamp) "</i><br/>
         <a href=\"" (str "blog_url") "\">[main]</a> | 
         <a href=\"" (str "blog_url") "/" (str "1278854531000") ".html\">[permalink]</a></body></html>") 
         (str "1278854531000.html")]] 
           (println innholdet tajm))

现在 REPL 给了我:

<html>
<head>
<title>title :: blog_title</title>
</head>
<body>
<h1>title</h1><br/>content<br/>
<i>posted Sun Jul 11 15:22:11 CEST 2010</i><br/>
<a href="blog_url">[main]</a> | 
<a href="blog_url/1278854531000.html">[permalink]</a>
</body></html> 
1278854531000.html
nil

再次,所有内容都已转移,以便在 REPL 中更容易阅读出现在一大串中。

这里的问题是,我可以在 REPL 中执行代码并获取 (Date.timestamp) 的值并且一切正常,但是当我在程序中的函数内部执行它时,我收到上述错误。如果有人能告诉我我在这里缺少什么,我将不胜感激。

I have the following code:

(defn post
  [title content timestamp]
    (let [[innholdet tajm]
       [(str "<html>
              <head>
              <title>" title " :: " blog_title "</title></head>
              <body><h1>" title "</h1>
              <br/>" content "<br/><i>posted " (Date. timestamp) "</i>
              <br/><a href=\"" (str blog_url) "\">[main]</a> | 
              <a href=\"" (str blog_url) "/" timestamp ".html\">[permalink]</a>
              </body></html>") (str timestamp".html")]]
                 (spit tajm innholdet)
   )
 )

I have another function that passes a title, the content and a UNIX timestamp to the function "post" above. blog_title and blog_url is var defined at the top of the code. I have sorted the HTML for aesthetics, in the "real code" this is no the case. I am also doing

(import 'java.util.Date)
(use 'clojure.string)

If I try to execute the code I get an error

java.lang.IllegalArgumentException 

If I remove (Date. timestamp) the code executes perfectly, but I need to have that (Date.) function.

Now, if in REPL I do

(import 'java.util.Date)

Then define a var with a timestamp

(def timestamp 1278854531000) ;; Just for pushing a var into (Date.) than just the number

And then I just copy the (let) into REPL and adjust the vars in the original code to rather just represent the different function names instead of the values (since this is the REPL and the vars, functions and values that are in the original code doesn't exist), to be like this:

(let [[innholdet tajm] 
  [(str "<html>
         <head>
         <title>" (str "title") " :: " (str "blog_title") "</title>
         </head>
         <body><h1>" (str "title") "</h1><br/>" (str "content") "<br/>
         <i>posted " (Date. timestamp) "</i><br/>
         <a href=\"" (str "blog_url") "\">[main]</a> | 
         <a href=\"" (str "blog_url") "/" (str "1278854531000") ".html\">[permalink]</a></body></html>") 
         (str "1278854531000.html")]] 
           (println innholdet tajm))

Now the REPL gives me:

<html>
<head>
<title>title :: blog_title</title>
</head>
<body>
<h1>title</h1><br/>content<br/>
<i>posted Sun Jul 11 15:22:11 CEST 2010</i><br/>
<a href="blog_url">[main]</a> | 
<a href="blog_url/1278854531000.html">[permalink]</a>
</body></html> 
1278854531000.html
nil

Again, everything has been shifted so that it would be more friendly to read, in REPL everything comes out in one large string.

The problem here is that I can execute the code in REPL and get the value of (Date. timestamp) and everything works out, but when I execute it inside of a function in my program I get the above mentioned error. Would appreciate if anyone could tell me what I am missing here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯狂的代价 2024-09-15 19:31:57

只需要进行一个小更改即可:

(defn post
  [title content timestamp]
    (let [[innholdet tajm]
       [(str "<html>
              <head>
              <title>" title " :: " blog_title "</title></head>
              <body><h1>" title "</h1>
              <br/>" content "<br/><i>posted " (Date. **(Long/parseLong timestamp)**) "</i>
              <br/><a href=\"" (str blog_url) "\">[main]</a> | 
              <a href=\"" (str blog_url) "/" timestamp ".html\">[permalink]</a>
              </body></html>") (str timestamp".html")]]
                 (spit tajm innholdet)
   )
 )

但是,我仍然建议您找到一种将时间戳作为长整型而不是字符串传递的方法。

Just a small change is necessary:

(defn post
  [title content timestamp]
    (let [[innholdet tajm]
       [(str "<html>
              <head>
              <title>" title " :: " blog_title "</title></head>
              <body><h1>" title "</h1>
              <br/>" content "<br/><i>posted " (Date. **(Long/parseLong timestamp)**) "</i>
              <br/><a href=\"" (str blog_url) "\">[main]</a> | 
              <a href=\"" (str blog_url) "/" timestamp ".html\">[permalink]</a>
              </body></html>") (str timestamp".html")]]
                 (spit tajm innholdet)
   )
 )

However, I would still recommend that you figure out a way to pass in the timestamp as a long instead of a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文