简体   繁体   English

Clojure - 用固定值 - >键功能映射或设置?

[英]Clojure - map or set with fixed value->key function?

I have quite a few records in my program that I end up putting in a map using one of their fields as key. 我的程序中有很多记录,我最终使用其中一个字段作为键放入地图。 For example 例如

(defrecord Foo. [id afield anotherfield])

And then I'd add that to a map with the id as key. 然后我将其添加到id为关键的地图中。 This is all perfectly doable, but a bit tedious, eg when adding a new instance of Foo to a map I need to extract the key first. 这一切都非常可行,但有点单调乏味,例如在向地图添加新的Foo实例时,我需要先提取密钥。 I'm wondering if somewhere in clojure.core a data structure to do this already exist? 我想知道clojure.core中的某个地方是否存在已经存在的数据结构?

Basically I'd like to construct a set of Foo's by giving the set a value to key mapping function (ie :id) at construction time of the set, and then have that used when I want to add/find/remove/... a value. 基本上我想通过在集合的构造时给组映射函数(即:id)设置一个值来构造一组Foo,然后在我想添加/查找/删除/时使用它。 。 一个值。

So instead of: 所以代替:

(assoc my-map (:id a-foo) a-foo))

I could do, say: 我能做到,说:

(conj my-set a-foo)

And more interestingly, merge and merge-with support. 更有趣的是,合并和合并 - 支持。

Sounds like a simple case where you would want to use a function to eliminate the "tedious" part. 听起来像一个简单的案例,你想要使用一个函数来消除“乏味”的部分。

eg 例如

(defn my-assoc [some-map some-record]
  (assoc some-map (:id some-record) some-record))

If you are doing this a lot and need different key functions, you might want to try a higher order function: 如果您正在执行此操作并需要不同的键功能,则可能需要尝试更高阶的功能:

(defn my-assoc-builder [id-function]
  (fn [some-map some-record] 
    (assoc some-map (id-function some-record) some-record)))

(def my-assoc-by-id (my-assoc-builder :id))

Finally, note that you could do the same with a macro. 最后,请注意您可以对宏执行相同的操作。 However a useful general rule with macros is not to use them unless you really need them . 然而,宏的一个有用的一般规则是不使用它们,除非你真的需要它们 Thus in this case, since it can be done easily with a function, I'd recommend sticking to functions. 因此,在这种情况下,由于可以通过功能轻松完成,我建议坚持使用功能。

Well as (AFAIK) there is no such datasctructure (and even if there were, it would probably do same tedious stuff in the background), you can build upon your record fns for desired operations (which will in background do same tedious stuff that needs to be done). 那么(AFAIK)就没有这样的数据结构(即使有,它可能会在背景中做同样繁琐的事情),你可以在你的记录fns上建立所需的操作(在后台会做同样繁琐的东西需要要完成)。

Basically I'd like to construct a set of Foo's by giving the set a value to key mapping function (ie :id) at construction time of the set, and then have that used when I want to add/find/remove/... 基本上我想通过在集合的构造时给组映射函数(即:id)设置一个值来构造一组Foo,然后在我想添加/查找/删除/时使用它。 。

Didn't get this.. If you are holding your records in a set and then want to eg find one by id you would have to do even more tidious work looking at every record until you find the right one.. that's O(n), and when using map you will have O(1). 没有得到这个..如果你在一个集合中持有你的记录,然后想要例如通过id找到一个你将不得不做更加整洁的工作查看每个记录,直到你找到正确的...那是O(n ),当使用地图时,你将有O(1)。 Did I use tedious too much? 我过分使用乏味吗? My suggestion is use map and do some tedious stuff.. It's all 1s and 0s after all :) 我的建议是使用地图并做一些乏味的事情......毕竟这都是1和0 ...

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

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