Created
December 5, 2014 16:50
-
-
Save jamesgarfield/8b12a25858797a233454 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type Collection []interface{} | |
func (c Collection) Len() int { | |
return len(c) | |
} | |
func (c Collection) Swap(i, j int) { | |
c[i], c[j] = c[j], c[i] | |
} | |
func (c Collection) All(fn func(interface{}) bool) bool { | |
for _, v := range c { | |
if !fn(v) { | |
return false | |
} | |
} | |
return true | |
} | |
func (c Collection) Any(fn func(interface{}) bool) bool { | |
for _, v := range c { | |
if fn(v) { | |
return true | |
} | |
} | |
return false | |
} | |
func (c Collection) Count(fn func(interface{}) bool) int { | |
count := 0 | |
for _, v := range c { | |
if fn(v) { | |
count += 1 | |
} | |
} | |
return count | |
} | |
func (c Collection) Each(fn func(interface{})) { | |
for _, v := range c { | |
fn(v) | |
} | |
} | |
func (c Collection) First(fn func(interface{}) bool) interface{} { | |
for _, v := range c { | |
if fn(v) { | |
return v | |
} | |
} | |
return nil | |
} | |
func (c Collection) Where(fn func(interface{}) bool) (result Collection) { | |
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