Add Filter() to List

And update collection templates to be backed by the untyped
Filter() implementations.

Fixes #578
This commit is contained in:
Chris Masone
2015-11-05 13:11:17 -08:00
parent c40b57fc41
commit b54fd91c82
4 changed files with 21 additions and 18 deletions

View File

@@ -134,11 +134,8 @@ func (l {{.Name}}) IterAllP(concurrency int, cb {{.Name}}IterAllCallback) {
type {{.Name}}FilterCallback func(v {{userType .ElemType}}, i uint64) (keep bool)
func (l {{.Name}}) Filter(cb {{.Name}}FilterCallback) {{.Name}} {
nl := New{{.Name}}()
l.IterAll(func(v {{userType .ElemType}}, i uint64) {
if cb(v, i) {
nl = nl.Append(v)
}
out := l.l.Filter(func(v {{$typesPackage}}Value, i uint64) bool {
return cb({{valueToUser "v" .ElemType}}, i)
})
return nl
return {{.Name}}{out, &ref.Ref{}}
}

View File

@@ -125,11 +125,8 @@ func (m {{.Name}}) IterAllP(concurrency int, cb {{.Name}}IterAllCallback) {
type {{.Name}}FilterCallback func(k {{userType .KeyType}}, v {{userType .ValueType}}) (keep bool)
func (m {{.Name}}) Filter(cb {{.Name}}FilterCallback) {{.Name}} {
nm := New{{.Name}}()
m.IterAll(func(k {{userType .KeyType}}, v {{userType .ValueType}}) {
if cb(k, v) {
nm = nm.Set(k, v)
}
out := m.m.Filter(func(k, v {{$typesPackage}}Value) bool {
return cb({{valueToUser "k" .KeyType}}, {{valueToUser "v" .ValueType}})
})
return nm
return {{.Name}}{out, &ref.Ref{}}
}

View File

@@ -105,13 +105,10 @@ func (s {{.Name}}) IterAllP(concurrency int, cb {{.Name}}IterAllCallback) {
type {{.Name}}FilterCallback func(p {{userType .ElemType}}) (keep bool)
func (s {{.Name}}) Filter(cb {{.Name}}FilterCallback) {{.Name}} {
ns := New{{.Name}}()
s.IterAll(func(v {{userType .ElemType}}) {
if cb(v) {
ns = ns.Insert(v)
}
out := s.s.Filter(func(v {{$typesPackage}}Value) bool {
return cb({{valueToUser "v" .ElemType}})
})
return ns
return {{.Name}}{out, &ref.Ref{}}
}
func (s {{.Name}}) Insert(p ...{{userType .ElemType}}) {{.Name}} {

View File

@@ -86,6 +86,18 @@ func (l List) iterInternal(sem chan int, lf listIterAllFunc, offset uint64) {
wg.Wait()
}
type listFilterCallback func(v Value, index uint64) (keep bool)
func (l List) Filter(cb listFilterCallback) List {
data := []Value{}
for i, v := range l.values {
if cb(v, uint64(i)) {
data = append(data, v)
}
}
return newListNoCopy(data, l.t)
}
type MapFunc func(v Value, index uint64) interface{}
func (l List) Map(mf MapFunc) []interface{} {