简体   繁体   English

在Clojure中,assocEx的作用是什么?

[英]In clojure, what is assocEx for?

I am implementing a custom data structure, that, for all intents and purposes, is a sorted map. 我正在实现一个自定义数据结构,出于所有目的和目的,它是一个排序的映射。 I wanted to program it so that I can use all of clojure's abstractions when it comes to maps, such as assoc , count , etc. In order to do that, I need to extend clojure's clojure.lang.IPersistentMap protocol to my new sorted map type. 我想对它进行编程,以便在涉及地图时可以使用clojure的所有抽象,例如assoccount等。为此,我需要将clojure的clojure.lang.IPersistentMap协议扩展到我的新排序地图上类型。

My question is this: What does assocEx do, or what is its purpose? 我的问题是: assocEx做什么的,或者它的目的是什么? Calling it on a normal clojure map shows that it differs from assoc in that it throws an exception if you try to insert a key/value pair where the key already exists in the map: 在普通clojure映射上调用它表明它与assoc不同之处在于,如果您尝试在映射中已存在该键的位置插入键/值对,则会引发异常:

user> (.assocEx (cast clojure.lang.IPersistentMap {:a :b}) :a 1)
RuntimeException Key already present  clojure.lang.Util.runtimeException (Util.java:219)

This is different from assoc , which simply replaces the old value in the map with the new one: 这与assoc不同,后者仅将地图中的旧值替换为新值:

user> (.assoc (cast clojure.lang.IPersistentMap {:a :b}) :a 1)
{:a 1}

But I can't seem to find a "clojure" function (one where the interop form/cast is not used above) that displays this functionality. 但是我似乎找不到找到显示此功能的“ clojure”功能(上面未使用互操作表单/ cast的功能)。 Where do we see the effects of this function using the normal clojure abstraction functions like assoc ? 使用assoc类的常规clojure抽象函数,我们在哪里可以看到此函数的效果?

Nothing ever uses assocEx anymore. 不再使用assocEx It is a remnant of an earlier time. 这是较早时期的残余。 If you are writing your own map type, you can implement (assocEx [mkv] (throw (Exception.))) . 如果要编写自己的地图类型,则可以实现(assocEx [mkv] (throw (Exception.)))

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

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