简体   繁体   English

你如何从clojure的瞬态地图中获取密钥?

[英]How do you get the keys from a transient map in clojure?

I am trying to get the seq of all of the keys in a transient map: 我试图在瞬态映射中获取所有键的seq:

(keys {3 4 5 6 7 8}) gives (3 5 7) (keys {3 4 5 6 7 8})给出(3 5 7)

as I expect but: 正如我所料,但是:

(keys (transient {3 4 5 6 7 8}))

gives

#<CompilerException java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.PersistentArrayMap$TransientArrayMap (NO_SOURCE_FILE:346)>

on the same note, how do I process using a transient map? 同样,我如何使用瞬态地图进行处理?

(map identity {3 4 5 6})

gives

([3 4] [5 6])

but

(map identity (transient {3 4 5 6}))

gives

#<IllegalArgumentException java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.PersistentArrayMap$TransientArrayMap>

This isn't really possible because of the interaction between laziness and mutability. 由于懒惰和可变性之间的相互作用,这实际上是不可能的。 (keys m) always returns a lazy sequence backed by the immutable m data structure, computing elements as necessary. (keys m)总是返回由不可变m数据结构支持的惰性序列,必要时计算元素。 But if m is a transient, it might change at any time, which would ruin the lazy key-sequence. 但是如果m是瞬态的,它可能随时改变,这会破坏懒惰的键序列。 You're really meant to not do anything too fancy with transients; 你真的打算不要做任何过于花哨的事情; and because it's so cheap to create transient or persistent versions of a data structure, it's not too onerous to go back and forth a few times if you really want to do something fancy. 并且因为创建数据结构的瞬态版本或持久版本非常便宜,如果你真的想要做一些奇特的事情,那么来回走动几次并不太繁琐。

I don't think there's a way to work on transients like you work on persistent structures. 我不认为有一种方法可以处理像你在持久结构上工作的瞬态。 You need to create a persistent structure from your transients with persistence! 您需要使用持久性从瞬态创建持久性结构persistence!

user> (map identity (persistent! (transient {3 4 5 6})))
([3 4] [5 6])

You can learn more about transients from Clojure docs . 您可以从Clojure文档中了解有关瞬态的更多信息。

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

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