refactor: rename Every to All

This commit is contained in:
Ruidy 2022-01-03 11:16:01 -04:00
parent b82129f655
commit 7460ba86a8
7 changed files with 36 additions and 38 deletions

View file

@ -6,8 +6,6 @@
![underscore](https://socialify.git.ci/rjNemo/underscore/image?description=1&font=Raleway&language=1&logo=https%3A%2F%2Fgithub.com%2FrjNemo%2Funderscore%2Fblob%2Fmain%2Fdocs%2Fstatic%2Flogo.png%3Fraw%3Dtrue&name=1&pattern=Floating%20Cogs&theme=Light) ![underscore](https://socialify.git.ci/rjNemo/underscore/image?description=1&font=Raleway&language=1&logo=https%3A%2F%2Fgithub.com%2FrjNemo%2Funderscore%2Fblob%2Fmain%2Fdocs%2Fstatic%2Flogo.png%3Fraw%3Dtrue&name=1&pattern=Floating%20Cogs&theme=Light)
`underscore` is a `Go` library providing useful functional programming helpers without extending any built-in objects. `underscore` is a `Go` library providing useful functional programming helpers without extending any built-in objects.
It is mostly a port from the `underscore.js` library based on generics brought by `go1.18`. It is mostly a port from the `underscore.js` library based on generics brought by `go1.18`.
@ -88,9 +86,9 @@ go test ./...
### Collections ### Collections
- `All`
- `Contains` (only numerics values at the moment) - `Contains` (only numerics values at the moment)
- `Each` - `Each`
- `Every`
- `Filter` - `Filter`
- `Find` - `Find`
- `Map` - `Map`
@ -105,7 +103,7 @@ go test ./...
Calling `chain.Of` will cause all future method calls to return wrapped values. When you've finished the computation, Calling `chain.Of` will cause all future method calls to return wrapped values. When you've finished the computation,
call `Value` to retrieve the final value. call `Value` to retrieve the final value.
Methods not returning a slice such as `Reduce`, `Every`, `Some`, will break the `Chain` and return `Value` instantly. Methods not returning a slice such as `Reduce`, `All`, `Some`, will break the `Chain` and return `Value` instantly.
## Built With ## Built With

View file

@ -1,8 +1,8 @@
package underscore package underscore
// Every returns true if all the values in the slice pass the predicate truth test. // All returns true if all the values in the slice pass the predicate truth test.
// Short-circuits and stops traversing the slice if a false element is found. // Short-circuits and stops traversing the slice if a false element is found.
func Every[T any](values []T, predicate func(T) bool) bool { func All[T any](values []T, predicate func(T) bool) bool {
for _, v := range values { for _, v := range values {
if !predicate(v) { if !predicate(v) {
return false return false

View file

@ -8,14 +8,14 @@ import (
u "github.com/rjNemo/underscore" u "github.com/rjNemo/underscore"
) )
func TestEvery(t *testing.T) { func TestAll(t *testing.T) {
nums := []int{1, 3, 5, 7, 9} nums := []int{1, 3, 5, 7, 9}
isOdd := func(n int) bool { return n%2 != 0 } isOdd := func(n int) bool { return n%2 != 0 }
assert.True(t, u.Every(nums, isOdd)) assert.True(t, u.All(nums, isOdd))
} }
func TestNotEvery(t *testing.T) { func TestNotAll(t *testing.T) {
nums := []int{1, 3, 5, 7, 9, 10} nums := []int{1, 3, 5, 7, 9, 10}
isOdd := func(n int) bool { return n%2 != 0 } isOdd := func(n int) bool { return n%2 != 0 }
assert.False(t, u.Every(nums, isOdd)) assert.False(t, u.All(nums, isOdd))
} }

View file

@ -13,7 +13,7 @@ type Chain[T constraints.Ordered] struct {
// Of starts a Chain. All future method calls will return Chain structs. When you've finished the computation, // Of starts a Chain. All future method calls will return Chain structs. When you've finished the computation,
// call Value to retrieve the final value. // call Value to retrieve the final value.
// //
// Methods not returning a slice such as Reduce, Every, Some, will break the chain and return Value instantly. // Methods not returning a slice such as Reduce, All, Some, will break the chain and return Value instantly.
func Of[T constraints.Ordered](value []T) Chain[T] { func Of[T constraints.Ordered](value []T) Chain[T] {
return Chain[T]{Value: value} return Chain[T]{Value: value}
} }
@ -29,11 +29,11 @@ func (c Chain[T]) Each(action func(T)) {
u.Each(c.Value, action) u.Each(c.Value, action)
} }
// Every returns true if all the values in the slice pass the predicate truth test. // All returns true if all the values in the slice pass the predicate truth test.
// Short-circuits and stops traversing the slice if a false element is found. // Short-circuits and stops traversing the slice if a false element is found.
// Breaks the Chain. // Breaks the Chain.
func (c Chain[T]) Every(predicate func(T) bool) bool { func (c Chain[T]) All(predicate func(T) bool) bool {
return u.Every(c.Value, predicate) return u.All(c.Value, predicate)
} }
// Filter looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate). // Filter looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate).

View file

@ -57,7 +57,7 @@ func TestChainFilterMapEvery(t *testing.T) {
assert.True(t, chain.Of(nums). assert.True(t, chain.Of(nums).
Filter(isEven). Filter(isEven).
Map(toSquare). Map(toSquare).
Every(func(n int) bool { return n%4 == 0 })) All(func(n int) bool { return n%4 == 0 }))
} }
func TestChainFilterMapFind(t *testing.T) { func TestChainFilterMapFind(t *testing.T) {

View file

@ -8,18 +8,18 @@ date: 2021-12-31T13:11:41-04:00
Calling `NewChain` will cause all future method calls to return wrapped objects. When you've finished the computation, Calling `NewChain` will cause all future method calls to return wrapped objects. When you've finished the computation,
call `Value` to retrieve the final value. call `Value` to retrieve the final value.
Methods not returning a collection such as `Reduce`, `Every`, `Some`, will break the chain and return `Value` instantly. Methods not returning a collection such as `Reduce`, `All`, `Some`, will break the chain and return `Value` instantly.
```go ```go
package main package main
import ( import (
"fmt" "fmt"
u "github.com/rjNemo/underscore" "github.com/rjNemo/underscore/chain"
) )
func main() { func main() {
sum := u.NewChain([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}). sum := chain.Of([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}).
// filter even numbers from the slice // filter even numbers from the slice
Filter(func(n int) bool { return n%2 == 0 }). Filter(func(n int) bool { return n%2 == 0 }).
// square every number in the slice // square every number in the slice

View file

@ -3,6 +3,26 @@ title: "Collections"
date: 2021-12-30T13:24:39-04:00 date: 2021-12-30T13:24:39-04:00
--- ---
## All
`All` returns true if all the values in the slice pass the predicate truth test.\
Short-circuits and stops traversing the slice if a false element is found.
```go
package main
import (
"fmt"
u "github.com/rjNemo/underscore"
)
func main() {
nums := []int{1, 3, 5, 7, 9}
isOdd := func(n int) bool { return n%2 != 0 }
fmt.Println(u.All(nums, isOdd)) // true
}
```
## Contains ## Contains
`Contains` returns true if the value is present in the slice. `Contains` returns true if the value is present in the slice.
@ -45,26 +65,6 @@ func main() {
} }
``` ```
## Every
`Every` returns true if all the values in the slice pass the predicate truth test.\
Short-circuits and stops traversing the slice if a false element is found.
```go
package main
import (
"fmt"
u "github.com/rjNemo/underscore"
)
func main() {
nums := []int{1, 3, 5, 7, 9}
isOdd := func(n int) bool { return n%2 != 0 }
fmt.Println(u.Every(nums, isOdd)) // true
}
```
## Filter ## Filter
`Filter` looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate). `Filter` looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate).