I am working on a problem to read in a file with lines like:
A abcdefg
B bcdefgh
Run Code Online (Sandbox Code Playgroud)
But I keep getting errors about Lazy Sequence not compatible with Java Charseq ..
I tried:
(def notlazy (doall lyne2))
Run Code Online (Sandbox Code Playgroud)
Then thought I verified:
(realized? notlazy)
true
Run Code Online (Sandbox Code Playgroud)
But still:
(str/split notlazy #" ")
ClassCastException class clojure.lang.LazySeq cannot be cast to class
java.lang.CharSequence (clojure.lang.LazySeq is in unnamed module of
loader 'app'; java.lang.CharSequence is in module java.base of loader
'bootstrap') clojure.string/split (string.clj:219)
Run Code Online (Sandbox Code Playgroud)
Help please!
with-redefs 函数似乎正是在 clojure 测试中模拟/存根依赖项所需要的。我正在使用 clojure.test [1.10.1]
最初它让我很伤心,因为在我运行测试时没有应用新的绑定。最后,我得到了以下设置按预期工作。其他命名空间需要依赖项
(ns abc
(:require [anotherns.id-gen-mock :as mock])
(deftest test-buy
(testing "Appends trade to the trades log"
(let [mock (atom {})]
(with-redefs [id-gen/get-next-id mock/get-next-id
save-trade (fn [t] (reset! mock {:trade t}))]
... test code
))))
Run Code Online (Sandbox Code Playgroud)
现在我意识到,模拟对我的所有测试都是通用的,所以我像这样升级了它。
(with-redefs [id-gen/get-next-id mock/get-next-id
save-trade identity]
(deftest test-holdings
(testing "after 1 buy"
... test code
Run Code Online (Sandbox Code Playgroud)
现在不使用新绑定,调用真正的依赖项 - 测试失败。
我在 SO 上看到一些帖子提到了有关“直接链接”的内容,但我无法真正理解为什么它在 Case1 中有效,而在 Case2 中无效。如果我将 with-redefs 移回 deftest 形式,它会再次工作。
在 Vim/GVim 中编辑 Clojure 代码时,我经常使用wword-motion 命令(以及它的表兄弟b向后的 word-motion)。然而,在默认的 Clojure 配置中,Vim/GVim 会忽略常见的单词分隔符,例如连字符、斜杠和句点。以下 Clojure 符号显示了所有三个:
clojure.core/select-keys
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们希望w和b字运动命令在任何非字母字符处停止。
如何修改默认的 Vim 配置以识别 Clojure 中的这些单词边界?
假设您在 clojure 中有以下代码行。您需要报价做什么?
(require '[clojure.string :as str])
Run Code Online (Sandbox Code Playgroud) 我对函数式语言非常陌生,我正在尝试在 Clojure 中实现一个简单的附加函数。我的代码:
(defn append
[lizt1 lizt2]
(cond
(empty? lizt1) lizt2
:else (def sq (cons (first sq lizt1) (append (rest sq lizt1) lizt2)))))
(let [x (quote (1 2 3))
y (quote (4 5 6))]
(append (x y))) )
Run Code Online (Sandbox Code Playgroud)
我收到了
clojure.lang.PersistentList cannot be cast to clojure.lang.IFn
Run Code Online (Sandbox Code Playgroud)
异常,所以我假设我尝试传递给函数的列表不是兼容类型(就像我要执行列表函数并尝试传递它一样)。我认为引用该列表将使其可以通过接口函数访问。有人可以帮助我概念化如何抽象列表以便在单独的函数中操作其数据吗?
我正在使用 Clojure、Emacs 和 Cider。
在 Cider REPL 中,该type函数返回以下内容:
user> (type true)
java.lang.Boolean
user> (type '(1 2))
clojure.lang.PersistentList
user> (type nil)
nil
Run Code Online (Sandbox Code Playgroud)
从更高层次的理解,我知道Clojure和Java之间存在集成。我认为这是 Clojure 导入 Java 库的一种方式。但是,联系似乎不止于此。
但是,为什么type有时会提到 Java 语言呢?为什么它引用了 Clojure 语言?为什么有时它根本没有提及任何语言(nil)?
我有一个Clojure功能:
(def obseq
(fn []
(let [a 0
b 1
c 2]
(println (seq '(a b c))))))
Run Code Online (Sandbox Code Playgroud)
它输出:
(a b c)
Run Code Online (Sandbox Code Playgroud)
我希望它输出一个包含a,b和c值的序列,而不是它们的名字.
期望的输出:
(1 2 3)
Run Code Online (Sandbox Code Playgroud)
我该如何实现?
如果我有一张看起来像的地图:
{:a 1 :b 2 :c 3}
Run Code Online (Sandbox Code Playgroud)
如何将其转换为向量,例如:
[:a 1 :b 2 :c 3]
Run Code Online (Sandbox Code Playgroud) 有没有更好的方法在 clojure 中实现嵌套循环?
作为初学者,我编写了这段嵌套循环代码,用于比较以天为单位的日期之间的差异。
将此与 Java 中使用 for 或 while 的嵌套循环进行比较。
(def my-vector [{:course-type "clojure"
:start-date "2021-01-25"
:end-date "2021-02-06"}
{:course-type "r"
:start-date "2021-01-15"
:end-date "2021-02-06"}
{:course-type "python"
:start-date "2020-12-05"
:end-date "2021-01-05"}
{:course-type "java"
:start-date "2020-09-15"
:end-date "2020-10-20"}
])
(defn find-gap-in-course [mycourses]
(println "Finding gap between dates....")
(loop [[course1 & mycourses] mycourses]
(loop [[course2 & mycourses] mycourses]
(when (and
(and (not-empty course1) (not-empty course2))
(> (-> java.time.temporal.ChronoUnit/DAYS
(.between
(LocalDate/parse (course2 :end-date))
(LocalDate/parse (course1 :start-date)))) 30))
(println "Dates evaluated are =" (course2 …Run Code Online (Sandbox Code Playgroud) (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?