Documentation ¶
Index ¶
- func All(in, predicateFn interface{}) (bool, error)
- func Any(in, predicateFn interface{}) (bool, error)
- func Every(in, predicateFn interface{}) (bool, error)
- func Filter(in, out, predicateFn interface{}) error
- func Find(in, out, predicateFn interface{}) error
- func Map(in, out, mapperFn interface{}) error
- func Reduce(in, out, reduceFn interface{}) error
- func Some(in, predicateFn interface{}) (bool, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All checks if predicate returns truthy for all element of collection. Iteration is stopped once predicate returns falsely. Currently, input of type slice is supported
Validations:
1. Predicate function should take one argument and return one value 2. Predicate function should return a bool value 3. Predicate function's argument should be of the same type as the elements of the input slice
Validation errors are returned to the caller
Example ¶
package main import ( "fmt" "github.com/thecasualcoder/godash" ) func main() { input := []int{0, 1, 2, 3, 4} output, _ := godash.All(input, func(num int) bool { return num >= 0 }) fmt.Println(output) }
Output: true
func Any ¶
Any checks if predicate returns truthy for any element of collection. Iteration is stopped once predicate returns truthy. Currently, input of type slice is supported
Validations:
1. Predicate function should take one argument and return one value 2. Predicate function should return a bool value 3. Predicate function's argument should be of the same type as the elements of the input slice
Validation errors are returned to the caller
Example ¶
package main import ( "fmt" "github.com/thecasualcoder/godash" ) func main() { input := []int{0, 1, 2, 3, 4} output, _ := godash.Any(input, func(num int) bool { return num%3 == 0 }) fmt.Println(output) }
Output: true
func Every ¶
Every is an alias for All function
Example ¶
package main import ( "fmt" "github.com/thecasualcoder/godash" ) func main() { input := []int{0, 1, 2, 3, 4} output, _ := godash.Every(input, func(num int) bool { return num >= 0 }) fmt.Println(output) }
Output: true
func Filter ¶
func Filter(in, out, predicateFn interface{}) error
Filter out elements that fail the predicate.
Input of type slice is supported as of now. Output is a slice in which filtered-in elements are stored. PredicateFn function is applied on each element of input to determine to filter or not
Validations:
- Input and Output's slice should be of same type
- Predicate function can take one argument and return one argument
- Predicate's return argument is always boolean.
- Predicate's input argument should be input/output slice's element type.
Validation errors are returned to the caller.
Example ¶
package main import ( "fmt" "github.com/thecasualcoder/godash" ) func main() { input := []string{ "rhythm", "of", "life", } var output []string _ = godash.Filter(input, &output, func(in string) bool { return len(in) > 3 }) fmt.Println(output) }
Output: [rhythm life]
func Find ¶
func Find(in, out, predicateFn interface{}) error
Find out elements.
Input of type slice is supported as of now. Output is a elements are matched. PredicateFn function is applied on each element of input to determine to find element until it finds the element
Validations:
- Input's element type and Output should be of same type
- Predicate function can take one argument and return one argument
- Predicate's return argument is always boolean.
- Predicate's input argument should be output element type.
Validation errors are returned to the caller
Example ¶
package main import ( "fmt" "strings" "github.com/thecasualcoder/godash" ) func main() { input := []string{ "rhythm", "of", "life", } var output string _ = godash.Find(input, &output, func(in string) bool { return strings.HasPrefix(in, "r") }) fmt.Println(output) }
Output: rhythm
func Map ¶
func Map(in, out, mapperFn interface{}) error
Map applies mapperFn on each item of in and puts it in out. Currently, input and output for type slice is supported.
Validations:
- Mapper function should take in one argument and return one argument
- Mapper function's argument should be of the same type of each element of input slice.
- Mapper function's output should be of the same type of each element of output slice.
Validation failures are returned as error by the godash.Map to the caller.
Example ¶
package main import ( "fmt" "github.com/thecasualcoder/godash" ) func main() { input := []int{0, 1, 2, 3, 4} var output []string _ = godash.Map(input, &output, func(num int) string { return fmt.Sprintf("%d", num*num) }) fmt.Println(output) }
Output: [0 1 4 9 16]
Example (Map) ¶
package main import ( "fmt" "github.com/thecasualcoder/godash" "sort" ) func main() { input := map[string]int{"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5} var output []int _ = godash.Map(input, &output, func(key string, num int) int { return num * num }) sort.Ints(output) fmt.Println(output) }
Output: [1 4 9 16 25]
func Reduce ¶
func Reduce(in, out, reduceFn interface{}) error
Reduce can accept a reducer and apply the reducer on each element of the input slice while providing an accumulator to save the reduce output.
Input of type slice is supported as of now. Output is the accumulator. ReduceFn is the reducer function.
Whatever ReduceFn returns is fed as accumulator for the next iteration. Reduction happens from left-to-right.
Reduce does the following validations:
- Reducer function should accept exactly 2 arguments and return 1 argument
- Reducer function's second argument should be the same type as input slice's element type
- Reducer function's return type should be the same as that of the accumulator
Validation errors are returned to the caller.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/thecasualcoder/godash" ) func main() { input := []string{"count", "words", "and", "print", "words", "count"} accumulator := map[string]int{} _ = godash.Reduce(input, &accumulator, func(acc map[string]int, element string) map[string]int { if _, present := acc[element]; present { acc[element] = acc[element] + 1 } else { acc[element] = 1 } return acc }) bytes, _ := json.MarshalIndent(accumulator, "", " ") fmt.Println(string(bytes)) }
Output: { "and": 1, "count": 2, "print": 1, "words": 2 }
Types ¶
This section is empty.