5 Star 2 Fork 0

Gitee 极速下载 / go-linq

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/ahmetalpbalkan/go-linq
克隆/下载
distinct.go 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
kalan 提交于 2016-08-29 23:57 . v2.0
package linq
// Distinct method returns distinct elements from a collection.
// The result is an unordered collection that contains no duplicate values.
func (q Query) Distinct() Query {
return Query{
Iterate: func() Iterator {
next := q.Iterate()
set := make(map[interface{}]bool)
return func() (item interface{}, ok bool) {
for item, ok = next(); ok; item, ok = next() {
if _, has := set[item]; !has {
set[item] = true
return
}
}
return
}
},
}
}
// Distinct method returns distinct elements from a collection.
// The result is an ordered collection that contains no duplicate values.
//
// NOTE: Distinct method on OrderedQuery type has better performance than
// Distinct method on Query type
func (oq OrderedQuery) Distinct() OrderedQuery {
return OrderedQuery{
orders: oq.orders,
Query: Query{
Iterate: func() Iterator {
next := oq.Iterate()
var prev interface{}
return func() (item interface{}, ok bool) {
for item, ok = next(); ok; item, ok = next() {
if item != prev {
prev = item
return
}
}
return
}
},
},
}
}
// DistinctBy method returns distinct elements from a collection. This method
// executes selector function for each element to determine a value to compare.
// The result is an unordered collection that contains no duplicate values.
func (q Query) DistinctBy(selector func(interface{}) interface{}) Query {
return Query{
Iterate: func() Iterator {
next := q.Iterate()
set := make(map[interface{}]bool)
return func() (item interface{}, ok bool) {
for item, ok = next(); ok; item, ok = next() {
s := selector(item)
if _, has := set[s]; !has {
set[s] = true
return
}
}
return
}
},
}
}
1
https://gitee.com/mirrors/go-linq.git
git@gitee.com:mirrors/go-linq.git
mirrors
go-linq
go-linq
v2.0.0-rc0

搜索帮助