Skip to content

Instantly share code, notes, and snippets.

@jamesgarfield
Created December 5, 2014 16:52
Show Gist options
  • Save jamesgarfield/4f967adcad71623a967b to your computer and use it in GitHub Desktop.
Save jamesgarfield/4f967adcad71623a967b to your computer and use it in GitHub Desktop.
package main
func (c Items) Len() int {
return len(c)
}
func (c Items) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c Items) All(fn func(*Item) bool) bool {
for _, v := range c {
if !fn(v) {
return false
}
}
return true
}
func (c Items) Any(fn func(*Item) bool) bool {
for _, v := range c {
if fn(v) {
return true
}
}
return false
}
func (c Items) Count(fn func(*Item) bool) int {
count := 0
for _, v := range c {
if fn(v) {
count += 1
}
}
return count
}
func (c Items) Each(fn func(*Item)) {
for _, v := range c {
fn(v)
}
}
func (c Items) First(fn func(*Item) bool) *Item {
for _, v := range c {
if fn(v) {
return v
}
}
return nil
}
func (c Items) Where(fn func(*Item) bool) (result Items) {
for _, v := range c {
if fn(v) {
result = append(result, v)
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment