Skip to content

Commit 8f72a58

Browse files
committed
- Remove output variable in all.go - Change README.md
1 parent 29c4925 commit 8f72a58

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ For more [docs](https://godoc.org/github.com/thecasualcoder/godash#All).
185185
```go
186186
func main() {
187187
input := []int{1, 2, 3, 4, 5}
188-
var output []int
188+
var output []bool
189189
output, _ := godash.All(input, func(num int) bool {
190190
return num >= 1
191191
})
@@ -199,7 +199,7 @@ func main() {
199199
{Name: "John", Age: 25},
200200
{Name: "Doe", Age: 15},
201201
}
202-
var output int
202+
var output bool
203203
output, _ := godash.Every(input, func(person Person) bool {
204204
return person.Age < 18
205205
})

all.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,50 @@ import (
1616
//
1717
// Validation errors are returned to the caller
1818
func All(in, predicateFn interface{}) (bool, error) {
19-
var output bool = true;
19+
2020
input := reflect.ValueOf(in)
2121
predicate := reflect.ValueOf(predicateFn)
2222

2323
if predicate.Kind() != reflect.Func {
24-
return output, fmt.Errorf("predicateFn has to be a function")
24+
return false, fmt.Errorf("predicateFn has to be a function")
2525
}
2626

2727
predicateFnType := predicate.Type()
2828
if predicateFnType.NumIn() != 1 {
29-
return output, fmt.Errorf("predicate function has to take only one argument")
29+
return false, fmt.Errorf("predicate function has to take only one argument")
3030
}
3131

3232
if predicateFnType.NumOut() != 1 {
33-
return output, fmt.Errorf("predicate function should return only one return value")
33+
return false, fmt.Errorf("predicate function should return only one return value")
3434
}
3535

3636
if predicateFnType.Out(0).Kind() != reflect.Bool {
37-
return output, fmt.Errorf("predicate function should return a boolean value")
37+
return false, fmt.Errorf("predicate function should return a boolean value")
3838
}
3939

4040
inputKind := input.Kind()
4141
if inputKind == reflect.Slice {
4242
inputSliceElemType := input.Type().Elem
4343
predicateFnArgType := predicateFnType.In(0)
4444
if inputSliceElemType() != predicateFnArgType {
45-
return output, fmt.Errorf("predicate function's argument (%s) has to be (%s)", predicateFnArgType, inputSliceElemType())
45+
return false, fmt.Errorf("predicate function's argument (%s) has to be (%s)", predicateFnArgType, inputSliceElemType())
4646
}
4747

4848
for i := 0; i < input.Len(); i++ {
4949
arg := input.Index(i)
5050
returnValue := predicate.Call([]reflect.Value{arg})[0]
5151
if !returnValue.Bool() {
52-
output = false
53-
break
52+
return false, nil
5453
}
5554
}
5655

57-
return output, nil
56+
return true, nil
5857
}
5958

60-
return output, fmt.Errorf("not implemented for (%s)", inputKind)
59+
return false, fmt.Errorf("not implemented for (%s)", inputKind)
6160
}
6261

6362
// Every is an alias for All function
6463
func Every(in, predicateFn interface{}) (bool, error) {
6564
return All(in, predicateFn)
66-
}
65+
}

0 commit comments

Comments
 (0)