Documentation ¶
Overview ¶
Package enumerable provides generic methods akin to the Enumerable module in ruby.
It's slower than writing them by hand since it uses the reflect package, but is quite readable.
Not that the other way isn't readable. Whatever, reflection FTW!
go get -v github.com/darkhelmet/enumerable
Index ¶
- func All(collection interface{}, predicate interface{}) bool
- func Any(collection interface{}, predicate interface{}) bool
- func Count(collection interface{}, predicate interface{}) int
- func Detect(collection interface{}, predicate interface{}) (interface{}, bool)
- func Map(collection interface{}, mapper interface{}) interface{}
- func None(collection interface{}, predicate interface{}) bool
- func Reduce(collection interface{}, initial interface{}, reducer interface{}) interface{}
- func Reject(collection interface{}, predicate interface{}) interface{}
- func Select(collection interface{}, predicate interface{}) interface{}
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
func All(collection interface{}, predicate interface{}) bool
All checks if all values in a collection satisfy a predicate function
Example ¶
package main import ( "fmt" "strings" E "github.com/darkhelmet/enumerable" ) func main() { words := strings.Fields("To be or not to be, that is the question!") okay := E.All(words, func(s string) bool { return len(s) < 10 }) fmt.Println(okay) }
Output: true
func Any ¶
func Any(collection interface{}, predicate interface{}) bool
Any checks if any values in a collection satisfy a predicate function
Example ¶
package main import ( "fmt" "strings" E "github.com/darkhelmet/enumerable" ) func main() { words := strings.Fields("To be or not to be, that is the question!") okay := E.Any(words, func(s string) bool { return len(s) > 2 }) fmt.Println(okay) }
Output: true
func Count ¶
func Count(collection interface{}, predicate interface{}) int
Count counts the number of values in a collection that satisfy a predicate
Example ¶
package main import ( "fmt" "strings" E "github.com/darkhelmet/enumerable" ) func main() { words := strings.Fields("To be or not to be, that is the question!") long := E.Count(words, func(s string) bool { return len(s) > 2 }) fmt.Println(long) }
Output: 5
func Detect ¶
func Detect(collection interface{}, predicate interface{}) (interface{}, bool)
Detect returns the first value in the collection that matches a predicate and true, or the zero value and false
Example ¶
package main import ( "fmt" "strings" E "github.com/darkhelmet/enumerable" ) func main() { words := strings.Fields("To be or not to be, that is the question!") value, ok := E.Detect(words, func(s string) bool { return len(s) > 2 }) fmt.Println(value, ok) }
Output: not true
Example (Notfound) ¶
package main import ( "fmt" E "github.com/darkhelmet/enumerable" ) func main() { ints := []int{1, 2, 3, 4, 5} value, ok := E.Detect(ints, func(i int) bool { return i > 10 }) fmt.Println(value, ok) }
Output: 0 false
func Map ¶
func Map(collection interface{}, mapper interface{}) interface{}
Map is a generic implementation of `map` of `map/reduce` fame
Example ¶
package main import ( "fmt" "strings" E "github.com/darkhelmet/enumerable" ) func main() { words := strings.Fields("To be or not to be, that is the question!") lengths := E.Map(words, func(s string) int { return len(s) }).([]int) fmt.Println(lengths) }
Output: [2 2 2 3 2 3 4 2 3 9]
func None ¶
func None(collection interface{}, predicate interface{}) bool
None checks that no values in a collection satisfy a predicate function
Example ¶
package main import ( "fmt" "strings" E "github.com/darkhelmet/enumerable" ) func main() { words := strings.Fields("To be or not to be, that is the question!") okay := E.None(words, func(s string) bool { return len(s) > 8 }) fmt.Println(okay) }
Output: false
func Reduce ¶
func Reduce(collection interface{}, initial interface{}, reducer interface{}) interface{}
Reduce is a generic implementation of `reduce` of `map/reduce` fame
Example (ImplicitInitialValue) ¶
package main import ( "fmt" E "github.com/darkhelmet/enumerable" ) func main() { ints := []int{1, 2, 3, 4, 5} sum := E.Reduce(ints, nil, func(memo, i int) int { return memo + i }).(int) fmt.Println(sum) }
Output: 15
Example (WithInitialValue) ¶
package main import ( "fmt" E "github.com/darkhelmet/enumerable" ) func main() { ints := []int{1, 2, 3, 4, 5} sum := E.Reduce(ints, 10, func(memo, i int) int { return memo + i }).(int) fmt.Println(sum) }
Output: 25
func Reject ¶
func Reject(collection interface{}, predicate interface{}) interface{}
Reject filters a collection to exclude those values that satisfy the predicate
Example ¶
package main import ( "fmt" "strings" E "github.com/darkhelmet/enumerable" ) func main() { words := strings.Fields("To be or not to be, that is the question!") values := E.Reject(words, func(s string) bool { return len(s) > 2 }) fmt.Println(values) }
Output: [To be or to is]
func Select ¶
func Select(collection interface{}, predicate interface{}) interface{}
Select filters a collection to only those values that satisfy the predicate
Example ¶
package main import ( "fmt" "strings" E "github.com/darkhelmet/enumerable" ) func main() { words := strings.Fields("To be or not to be, that is the question!") values := E.Select(words, func(s string) bool { return len(s) > 2 }) fmt.Println(values) }
Output: [not be, that the question!]
Types ¶
This section is empty.