Clojure中的自定义相等性 [英] Custom equality in Clojure distinct

查看:48
本文介绍了Clojure中的自定义相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Clojure程序中,我得到了一个由包含人们姓名和电子邮件的地图组成的数组。

In a Clojure program I've an array composed by maps containing peoples' names and emails.

例如

[
    { :name "John" :email "john@gmail.com" }  
    { :name "Batman" :email "batman@gmail.com" }  
    { :name "John Doe" :email "john@gmail.com" }  
 ] 

我想删除重复的条目,考虑到出于比较目的,具有相同的电子邮件对的邮件对应该相等。在上面的示例中,输出为:

I'd like to remove the duplicate entries considering, for comparison purposes, pairs having the same e-mail to be equals. In the example above the output would be:

[
    { :name "John" :email "john@gmail.com" }  
    { :name "Batman" :email "batman@gmail.com" }  
 ] 

在Clojure中实现此目标的最佳方法是什么?有没有一种方法可以让众人知道要使用什么等于函数?

What's the best way to achieve this in Clojure? Is there a way to let distinct knows what equals function to use?

谢谢。

推荐答案

这可以做到: https://crossclj.info/fun/medley.core /distinct-by.html

链接中的函数会懒惰地遍历每个值并存储所看到的所有内容。如果已经看到 col 中的值,则不会添加它。

The function in the link goes through every value lazily and stores everything it's seen. If the value in the coll is already seen, it does not add it.

您可以将其称为:(与#(%:email)地图区分开),其中 maps 是您的人脉地图矢量。

You could then call this as: (distinct-by #(% :email) maps), where maps is your vector of people-maps.

这篇关于Clojure中的自定义相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆