在 Clojure 中递归集合的惯用方法
我试图了解 Clojure 中通过树或由 Clojure 列表(或其他集合类型)表示的列表进行递归的惯用方式是什么。
我可以编写以下代码来计算平面集合中的元素(忽略它不是尾递归的事实):
(defn length
([xs]
(if (nil? (seq xs))
0
(+ 1 (length (rest xs))))))
现在在Scheme或CL中,所有示例仅在列表上执行此操作,因此这些语言中的惯用基本案例测试将为(nil?xs)
。在 Clojure 中,我们希望这个函数适用于所有集合类型,惯用的测试也是如此:(nil? (seq xs))
,或者 (empty? xs)
,或者完全不同的东西?
我想考虑的另一种情况是树遍历,即遍历表示树的列表或向量,例如[1 2 [3 4]
。
例如,计算树中的节点:
(defn node-count [tree]
(cond (not (coll? tree)) 1
(nil? (seq tree)) 0
:else (+ (node-count (first tree)) (node-count (rest tree)))))
这里我们使用(not (coll?tree))
来检查原子,而在Scheme/CL中我们会使用atom?
>。我们还使用 (nil? (seq tree))
来检查空集合。最后,我们使用 first
和 rest
将当前树解构为左分支和树的其余部分。
总而言之,以下形式在 Clojure 中是惯用的:
(nil? (seq xs))
用于测试空集合(first xs)
和(rest xs)
深入挖掘集合(不是 (coll? xs))
检查原子
I'm trying to understand what is the idiomatic way in Clojure to recurse through a tree or a list represented by a Clojure list (or another collection type).
I could write the following to count the elements in a flat collection (ignore the fact that it's not tail-recursive):
(defn length
([xs]
(if (nil? (seq xs))
0
(+ 1 (length (rest xs))))))
Now in Scheme or CL all the examples only ever do this over lists, so the idiomatic base case test in those languages would be (nil? xs)
. In Clojure we'd like this function to work on all collection types, so is the idiomatic test (nil? (seq xs))
, or maybe (empty? xs)
, or something completely different?
The other case I'd like to consider is tree traversal, i.e. traversing through a list or vector that represents a tree, e.g. [1 2 [3 4]
.
For example, counting the nodes in a tree:
(defn node-count [tree]
(cond (not (coll? tree)) 1
(nil? (seq tree)) 0
:else (+ (node-count (first tree)) (node-count (rest tree)))))
Here we use (not (coll? tree))
to check for atoms, whereas in Scheme/CL we'd use atom?
. We also use (nil? (seq tree))
to check for an empty collection. And finally we use first
and rest
to destructure the current tree to the left branch and the rest of the tree.
So to summarise, are the following forms idiomatic in Clojure:
(nil? (seq xs))
to test for the empty collection(first xs)
and(rest xs)
to dig into the collection(not (coll? xs))
to check for atoms
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非空 seqable 的惯用测试是
(seq coll)
:nil?
是不必要的,因为非nil
返回值seq
保证是一个 seq,因此既不是nil
也不是false
,因此是 true。如果您想先处理
nil
情况,可以将if
更改为if-not
或seq
为空?
;后者被实现为seq
和not
的组合(这就是为什么编写(not (empty? xs))
并不符合惯用语,参见空?
的文档字符串)。至于
first
/rest
——记住rest
的严格变体next
很有用,使用这比将rest
包装在seq
中更惯用。最后,
coll?
检查其参数是否是 Clojure 持久集合(clojure.lang.IPercientCollection
的实例)。这是否是对“非原子”的适当检查取决于代码是否需要将 Java 数据结构作为非原子处理(通过互操作):例如(coll? (java.util.HashSet.)) 为
false
,(coll? (into-array []))
也是如此,但您可以对两者调用seq
。core.incubator
中有一个名为seqable?
的函数 在新的模块化贡献中,它承诺确定(seq x)
对于给定的x
是否会成功。The idiomatic test for a non-empty seqable is
(seq coll)
:The
nil?
is unnecessary, since a non-nil
return value fromseq
is guaranteed to be a seq and thus neithernil
norfalse
and therefore truthy.If you want to deal with the
nil
case first, you can change theif
toif-not
orseq
toempty?
; the latter is implemented as a composition ofseq
withnot
(which is why it is not idiomatic to write(not (empty? xs))
, cf. the docstring ofempty?
).As for
first
/rest
-- it's useful to remember about the strict variant ofrest
,next
, the use of which is more idiomatic than wrappingrest
in aseq
.Finally,
coll?
checks if its argument is a Clojure persistent collection (an instance ofclojure.lang.IPersistentCollection
). Whether this is an appropriate check for "non-atoms" depends on whether the code needs to handle Java data structures as non-atoms (via interop): e.g.(coll? (java.util.HashSet.))
isfalse
, as is(coll? (into-array []))
, but you can callseq
on both. There is a function calledseqable?
incore.incubator
in the new modular contrib which promises to determine whether(seq x)
would succeed for a givenx
.我个人喜欢以下通过集合递归的方法:
特点:
请注意,通常最好使用 recur 编写递归 如果可能的话,这样您就可以获得尾递归的好处,并且不会冒炸毁堆栈的风险。因此,考虑到这一点,我实际上可能会编写这个特定的函数,如下所示:
I personally like the following approach to recurse through a collection:
Features:
Note that in general it is better to write recursive functions using recur if possible, so that you get the benefits of tail recursion and don't risk blowing up the stack. So with this in mind, I'd actually probably write this specific function as follows: