4clojure 题解 Set Intersection

作者: admin 日期: 2017-12-29 16:15:31 人气: - 评论: 0

求两个set的交集合,不允许使用Intersection


Write a function which returns the intersection of two sets. The intersection is the sub-set of items that each set has in common.

test not run
(= (__ #{0 1 2 3} #{2 3 4 5}) #{2 3})
test not run
(= (__ #{0 1 2} #{3 4 5}) #{})
test not run
(= (__ #{:a :b :c :d} #{:c :e :a :f :d}) #{:a :c :d})











参考了clojure.set/intersection 的实现


(fn [s1 s2]
(reduce (fn [result item]
(if (contains? s2 item)
result
(disj result item))
) s1 s1)
)


disj 是一个从集合中去除指定项目返回一个新集合的操作

相关内容

发表评论
更多 网友评论0 条评论)
暂无评论

Copyright © 2012-2014 我的代码板 Inc. 保留所有权利。

页面耗时0.0273秒, 内存占用1.83 MB, 访问数据库13次

闽ICP备15009223号-1