Skip to content

Commit 506df2e

Browse files
arunvelsriramdineshba
authored andcommitted
Implement "Some" function
1 parent 45b2e91 commit 506df2e

File tree

3 files changed

+104
-81
lines changed

3 files changed

+104
-81
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This library heavily makes use of `reflect` package and hence will have an **imp
1717
1. [Map](#Map)
1818
2. [Filter](#Filter)
1919
3. [Reduce](#Reduce)
20-
4. [Any](#Any)
20+
4. [Any](#Any-or-Some) or [Some](#Any-or-Some)
2121

2222
## Usages
2323

@@ -143,9 +143,9 @@ func main() {
143143
}
144144
```
145145

146-
### Any
146+
### Any or Some
147147

148-
Any checks if predicate returns truthy for any element of collection. Iteration is stopped once predicate returns truthy.
148+
Any or Some checks if predicate returns truthy for any element of collection. Iteration is stopped once predicate returns truthy.
149149
For more [docs](https://godoc.org/github.com/thecasualcoder/godash#Any).
150150

151151
```go
@@ -169,7 +169,7 @@ func main() {
169169
}
170170
var output int
171171

172-
output, _ := godash.Any(input, func(person Person) bool {
172+
output, _ := godash.Some(input, func(person Person) bool {
173173
return person.Age < 18
174174
})
175175

any.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ func Any(in, predicateFn interface{}) (bool, error) {
5858

5959
return output, fmt.Errorf("not implemented for (%s)", inputKind)
6060
}
61+
62+
// Some is an alias for Any function
63+
func Some(in, predicateFn interface{}) (bool, error) {
64+
return Any(in, predicateFn)
65+
}

any_test.go

Lines changed: 95 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,118 +7,136 @@ import (
77
"testing"
88
)
99

10-
func TestAny(t *testing.T) {
10+
func TestSomeAndAny(t *testing.T) {
11+
var funcs = map[string]func(interface{}, interface{}) (bool, error){
12+
"Any()": godash.Any,
13+
"Some()": godash.Some,
14+
}
1115

12-
t.Run("should return err if predicate is not a function", func(t *testing.T) {
13-
in := []int{1, 2, 3}
16+
for fnName, fn := range funcs {
17+
t.Run(fmt.Sprintf("%s should return err if predicate is not a function", fnName), func(t *testing.T) {
18+
in := []int{1, 2, 3}
1419

15-
_, err := godash.Any(in, "not a func")
20+
_, err := fn(in, "not a func")
1621

17-
assert.EqualError(t, err, "predicateFn has to be a function")
18-
})
22+
assert.EqualError(t, err, "predicateFn has to be a function")
23+
})
1924

20-
t.Run("should return err if predicate function do not take exactly one argument", func(t *testing.T) {
21-
in := []int{1, 2, 3}
25+
t.Run(fmt.Sprintf("%s should return err if predicate function do not take exactly one argument", fnName), func(t *testing.T) {
26+
in := []int{1, 2, 3}
2227

23-
{
24-
_, err := godash.Any(in, func() {})
28+
{
29+
_, err := fn(in, func() {})
2530

26-
assert.EqualError(t, err, "predicate function has to take only one argument")
27-
}
28-
{
29-
_, err := godash.Any(in, func(int, int) {})
31+
assert.EqualError(t, err, "predicate function has to take only one argument")
32+
}
33+
{
34+
_, err := fn(in, func(int, int) {})
3035

31-
assert.EqualError(t, err, "predicate function has to take only one argument")
32-
}
33-
})
36+
assert.EqualError(t, err, "predicate function has to take only one argument")
37+
}
38+
})
3439

35-
t.Run("should return err if predicate function do not return exactly one value", func(t *testing.T) {
36-
in := []int{1, 2, 3}
40+
t.Run(fmt.Sprintf("%s should return err if predicate function do not return exactly one value", fnName), func(t *testing.T) {
41+
in := []int{1, 2, 3}
3742

38-
{
39-
_, err := godash.Any(in, func(int) {})
43+
{
44+
_, err := fn(in, func(int) {})
4045

41-
assert.EqualError(t, err, "predicate function should return only one return value")
42-
}
43-
{
44-
_, err := godash.Any(in, func(int) (bool, bool) { return true, true })
46+
assert.EqualError(t, err, "predicate function should return only one return value")
47+
}
48+
{
49+
_, err := fn(in, func(int) (bool, bool) { return true, true })
4550

46-
assert.EqualError(t, err, "predicate function should return only one return value")
51+
assert.EqualError(t, err, "predicate function should return only one return value")
4752

48-
}
49-
})
53+
}
54+
})
5055

51-
t.Run("should return err if predicate function's return value is not a boolean", func(t *testing.T) {
52-
in := []int{1, 2, 3}
56+
t.Run(fmt.Sprintf("%s should return err if predicate function's return value is not a boolean", fnName), func(t *testing.T) {
57+
in := []int{1, 2, 3}
5358

54-
_, err := godash.Any(in, func(int) int { return 0 })
59+
_, err := fn(in, func(int) int { return 0 })
5560

56-
assert.EqualError(t, err, "predicate function should return a boolean value")
57-
})
61+
assert.EqualError(t, err, "predicate function should return a boolean value")
62+
})
5863

59-
t.Run("should return err if input is not a slice", func(t *testing.T) {
60-
in := 1
64+
t.Run(fmt.Sprintf("%s should return err if input is not a slice", fnName), func(t *testing.T) {
65+
in := 1
6166

62-
_, err := godash.Any(in, func(int) bool { return true })
67+
_, err := fn(in, func(int) bool { return true })
6368

64-
assert.EqualError(t, err, "not implemented for (int)")
65-
})
69+
assert.EqualError(t, err, "not implemented for (int)")
70+
})
6671

67-
t.Run("should return err if there is a type mismatch between predicate function's argument and input slice", func(t *testing.T) {
68-
in := []string{"hello", "world"}
72+
t.Run(fmt.Sprintf("%s should return err if there is a type mismatch between predicate function's argument and input slice", fnName), func(t *testing.T) {
73+
in := []string{"hello", "world"}
6974

70-
_, err := godash.Any(in, func(int) bool { return true })
75+
_, err := fn(in, func(int) bool { return true })
7176

72-
assert.EqualError(t, err, "predicate function's argument (int) has to be (string)")
73-
})
77+
assert.EqualError(t, err, "predicate function's argument (int) has to be (string)")
78+
})
7479

75-
t.Run("should return true if predicate passes for any of the element in input slice", func(t *testing.T) {
76-
in := []int{1, 1, 2, 3, 5, 8, 13}
80+
t.Run(fmt.Sprintf("%s should return true if predicate passes for at least one of the element in input slice", fnName), func(t *testing.T) {
81+
in := []int{1, 1, 2, 3, 5, 8, 13}
7782

78-
output, err := godash.Any(in, func(elem int) bool { return elem%5 == 0 })
83+
output, err := fn(in, func(elem int) bool { return elem%5 == 0 })
7984

80-
assert.NoError(t, err)
81-
assert.True(t, output)
82-
})
85+
assert.NoError(t, err)
86+
assert.True(t, output)
87+
})
8388

84-
t.Run("should return false if predicate fails for all the elements in input slice", func(t *testing.T) {
85-
in := []int{1, 1, 2, 3, 5, 8, 13}
89+
t.Run(fmt.Sprintf("%s should return false if predicate fails for all the elements in input slice", fnName), func(t *testing.T) {
90+
in := []int{1, 1, 2, 3, 5, 8, 13}
8691

87-
output, err := godash.Any(in, func(num int) bool { return num%6 == 0 })
92+
output, err := fn(in, func(num int) bool { return num%6 == 0 })
8893

89-
assert.NoError(t, err)
90-
assert.False(t, output)
91-
})
94+
assert.NoError(t, err)
95+
assert.False(t, output)
96+
})
97+
98+
t.Run(fmt.Sprintf("%s should support structs", fnName), func(t *testing.T) {
99+
type person struct {
100+
name string
101+
age int
102+
}
103+
in := []person{
104+
{name: "John", age: 12},
105+
{name: "Doe", age: 25},
106+
}
107+
108+
{
109+
output, err := fn(in, func(person person) bool { return person.age < 18 })
110+
111+
assert.NoError(t, err)
112+
assert.True(t, output)
113+
}
114+
{
115+
output, err := fn(in, func(person person) bool { return person.age < 30 })
116+
117+
assert.NoError(t, err)
118+
assert.True(t, output)
119+
}
120+
})
121+
}
122+
}
92123

93-
t.Run("should support structs", func(t *testing.T) {
94-
type person struct {
95-
name string
96-
age int
97-
}
98-
in := []person{
99-
{name: "John", age: 12},
100-
{name: "Doe", age: 25},
101-
}
124+
func ExampleAny() {
125+
input := []int{0, 1, 2, 3, 4}
102126

103-
{
104-
output, err := godash.Any(in, func(person person) bool { return person.age < 18 })
127+
output, _ := godash.Any(input, func(num int) bool {
128+
return num%3 == 0
129+
})
105130

106-
assert.NoError(t, err)
107-
assert.True(t, output)
108-
}
109-
{
110-
output, err := godash.Any(in, func(person person) bool { return person.age < 30 })
131+
fmt.Println(output)
111132

112-
assert.NoError(t, err)
113-
assert.True(t, output)
114-
}
115-
})
133+
// Output: true
116134
}
117135

118-
func ExampleAny() {
136+
func ExampleSome() {
119137
input := []int{0, 1, 2, 3, 4}
120138

121-
output, _ := godash.Any(input, func(num int) bool {
139+
output, _ := godash.Some(input, func(num int) bool {
122140
return num%3 == 0
123141
})
124142

0 commit comments

Comments
 (0)