标签: clojure

为什么循环语句的绑定向量数量是奇数?

我一直在查看示例和文档;我不明白为什么我会得到

loop requires an even number of forms in binding vector错误。

我相信问题出在循环和初始化使用索引中。我只是不明白我做错了什么。

(def usage-vec-len 13)

(defn ret-usage-indicies
"Takes a billed water consumption amount, and returns a vector of the ranges."
[reading]
(let [usage-indicies [0 0 0 0 0 0 0 0 0 0 0 0 0] curr-idx (- usage-vec-len 1)]
 (loop [curr-reading reading ui usage-indicies curr-idx]
.
.
.
 (if (= remaining-reading 0)
 ui
 (recur remaining-reading (assoc ui curr-idx curr-reading) (dec curr-idx)))))))
Run Code Online (Sandbox Code Playgroud)

clojure

0
推荐指数
1
解决办法
1051
查看次数

使用不同的装置多次运行测试

如果我通过use-fixtures多个灯具,它会一个接一个地关闭它们:

(def ^:dynamic *path* nil)

(defn sun [f]
  (println "sun setup" *path*)
  (binding [*path* "sun"]
    (f))
  (println "sun cleanup"))

(defn rain [f]
  (println "rain setup" *path*)
  (binding [*path* "rain"]
    (f))
  (println "rain cleanup"))

(use-fixtures :once sun rain)

(deftest sometest1
         (println "sometest1" *path*))

(deftest sometest2
         (println "sometest2" *path*))

(run-tests)

Testing scratchpad.core
sun setup nil
rain setup sun
sometest1 rain
sometest2 rain
rain cleanup
sun cleanup
Run Code Online (Sandbox Code Playgroud)

这很有用,但如何做类似的事情:

Testing scratchpad.core
sun setup nil
sometest1 sun
sometest2 sun
sun cleanup
rain setup …
Run Code Online (Sandbox Code Playgroud)

clojure

0
推荐指数
1
解决办法
619
查看次数

0
推荐指数
1
解决办法
76
查看次数

在 Clojure 中编写中缀宏时出现问题

我正在尝试编写 Clojure 中缀宏,但出现我不明白的编译错误。

它应该从正则数学表达式语法生成函数调用:

(macroexpand '(infix 3 * (2 + 1)))
;; => (* 3 (+ 2 1))
Run Code Online (Sandbox Code Playgroud)

我尝试将list语句转换为引用,但没有成功。

宏:

(defmacro functionize [macro]
  `(fn [& args#] (eval (cons '~macro args#))))

(defmacro infix
  ([n]
   (if (not (or (number? n) (fn? n)))
     `(~(apply (functionize infix) n))
     n))
  ([fir sec & res]
   (list sec (infix fir) (infix res))))
Run Code Online (Sandbox Code Playgroud)

错误:

1. Caused by java.lang.IllegalArgumentException
   Don't know how to create ISeq from: clojure.lang.Symbol
Run Code Online (Sandbox Code Playgroud)

错误发生在最后一行,第一次调用infix.

clojure

0
推荐指数
1
解决办法
316
查看次数

next-jdbc:执行多个语句?

我正在使用新seancorfield/next.jdbc库在 clojure 中编写一个简单的数据库迁移代码。

如何同时执行多条SQL语句?用例是我有一个 SQL 文件,其中包含用于从一个版本迁移到下一个版本的查询代码。next.jdbc/execute!按照设计只执行一条语句。

jdbc clojure

0
推荐指数
1
解决办法
485
查看次数

在 Clojure 中的命名空间中隐藏核心函数

我正在尝试隐藏我的命名空间之一中的核心函数。代码的工作原理如下:

核心.clj:

(ns test.core
  (:refer-clojure :exclude [zero?])
  (:require [test.z :refer [zero?]]))

(defn -main
  (prn (zero? 1)))
Run Code Online (Sandbox Code Playgroud)

z.clj:

(ns test.z
  (:refer-clojure :exclude [zero?]))
(defn zero? [x] (+ x 1))
Run Code Online (Sandbox Code Playgroud)

运行时,显示错误信息:

; (err) WARNING: zero? already refers to: #'clojure.core/zero? in namespace: test.z, being replaced by: #'test.z/zero?
Run Code Online (Sandbox Code Playgroud)

我不确定 Clojure 是否允许这样做,有人可以帮助我吗?多谢!


编辑:这似乎是问题所在,但错误消息确实令人困惑。不管怎样,非常感谢!

clojure

0
推荐指数
1
解决办法
384
查看次数

否定参数:“无法解析符号:-x 在此上下文中”

(defn my-fun [x]
    (println -x))
Run Code Online (Sandbox Code Playgroud)

执行此代码,我得到:

Unable to resolve symbol: -x in this context
Run Code Online (Sandbox Code Playgroud)

为什么我不能只反转 x?

clojure

0
推荐指数
1
解决办法
60
查看次数

如何计算一个非常大的惰性序列中的元素数量?

我有以下(非常大)lazy-seq:

(def lazy-list (partition-all 100000 (take 10000000000000000000 (repeat 1))))
Run Code Online (Sandbox Code Playgroud)

我想计算其中的元素数量,为此我正在执行以下操作:

(time
  (loop [ll lazy-list
         c 0]
    (if-not (seq (take 1 ll))
      c
      (recur (drop 1 ll)
             (inc c)))))
Run Code Online (Sandbox Code Playgroud)

如果我运行这个,我会收到以下错误:

Execution error (OutOfMemoryError) at user/eval2043$fn (REPL:1).
Java heap space
Run Code Online (Sandbox Code Playgroud)

但如果我没有持有head任何地方,为什么我会看到这个OOM问题?

memory clojure out-of-memory large-data lazy-sequences

0
推荐指数
1
解决办法
140
查看次数

为什么时间需要是一个宏?

Clojure源代码给出了以下定义time

(defmacro time
  "Evaluates expr and prints the time it took.  Returns the value of
 expr."
  {:added "1.0"}
  [expr]
  `(let [start# (. System (nanoTime))
         ret# ~expr]
     (prn (str "Elapsed time: " (/ (double (- (. System (nanoTime)) start#)) 1000000.0) " msecs"))
     ret#))
Run Code Online (Sandbox Code Playgroud)

为什么这需要一个宏?函数无法完成怎么办?

macros time clojure

0
推荐指数
1
解决办法
92
查看次数

在 clojure 字符串列表中查找子字符串

我想查找我的字符串列表中的任何字符串中是否有子字符串。我有一个像'(“hi”“hey”“hello”)这样的列表。使用“some”我可以找到值“hi”是否在此列表中。但是我怎样才能找到列表中的至少一个字符串中是否只有“h”呢?

clojure clojurescript

0
推荐指数
1
解决办法
622
查看次数