refactor: rename Some to Any

This commit is contained in:
Ruidy 2022-01-03 11:20:36 -04:00
parent 7460ba86a8
commit 81c98e8bf5
7 changed files with 48 additions and 48 deletions

View file

@ -87,6 +87,7 @@ go test ./...
### Collections ### Collections
- `All` - `All`
- `Any`
- `Contains` (only numerics values at the moment) - `Contains` (only numerics values at the moment)
- `Each` - `Each`
- `Filter` - `Filter`
@ -96,14 +97,13 @@ go test ./...
- `Min` - `Min`
- `Partition` - `Partition`
- `Reduce` - `Reduce`
- `Some`
### Chaining ### Chaining
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`, `All`, `Some`, will break the `Chain` and return `Value` instantly. Methods not returning a slice such as `Reduce`, `All`, `Any`, will break the `Chain` and return `Value` instantly.
## Built With ## Built With

View file

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

View file

@ -12,12 +12,12 @@ func TestSome(t *testing.T) {
nums := []int{1, 2, 4, 6, 8} nums := []int{1, 2, 4, 6, 8}
isOdd := func(n int) bool { return n%2 != 0 } isOdd := func(n int) bool { return n%2 != 0 }
assert.True(t, u.Some(nums, isOdd)) assert.True(t, u.Any(nums, isOdd))
} }
func TestNotSome(t *testing.T) { func TestNotSome(t *testing.T) {
nums := []int{2, 4, 6, 8} nums := []int{2, 4, 6, 8}
isOdd := func(n int) bool { return n%2 != 0 } isOdd := func(n int) bool { return n%2 != 0 }
assert.False(t, u.Some(nums, isOdd)) assert.False(t, u.Any(nums, isOdd))
} }

View file

@ -13,11 +13,25 @@ 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, All, Some, will break the chain and return Value instantly. // Methods not returning a slice such as Reduce, All, Any, 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}
} }
// 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.
// Breaks the Chain.
func (c Chain[T]) All(predicate func(T) bool) bool {
return u.All(c.Value, predicate)
}
// Any returns true if any of the values in the slice pass the predicate truth test.
// Short-circuits and stops traversing the slice if a true element is found.
// Breaks the Chain.
func (c Chain[T]) Any(predicate func(T) bool) bool {
return u.Any(c.Value, predicate)
}
// Contains returns true if the value is present in the slice and breaks the Chain. // Contains returns true if the value is present in the slice and breaks the Chain.
func (c Chain[T]) Contains(value T) bool { func (c Chain[T]) Contains(value T) bool {
return u.Contains(c.Value, value) return u.Contains(c.Value, value)
@ -29,13 +43,6 @@ func (c Chain[T]) Each(action func(T)) {
u.Each(c.Value, action) u.Each(c.Value, action)
} }
// 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.
// Breaks the Chain.
func (c Chain[T]) All(predicate func(T) bool) bool {
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).
func (c Chain[T]) Filter(predicate func(n T) bool) Chain[T] { func (c Chain[T]) Filter(predicate func(n T) bool) Chain[T] {
return Chain[T]{Value: u.Filter(c.Value, predicate)} return Chain[T]{Value: u.Filter(c.Value, predicate)}
@ -85,10 +92,3 @@ func (c Chain[T]) Partition(predicate func(T) bool) ([]T, []T) {
func (c Chain[T]) Reduce(reducer func(n, acc T) T, acc T) T { func (c Chain[T]) Reduce(reducer func(n, acc T) T, acc T) T {
return u.Reduce(c.Value, reducer, acc) return u.Reduce(c.Value, reducer, acc)
} }
// Some returns true if any of the values in the slice pass the predicate truth test.
// Short-circuits and stops traversing the slice if a true element is found.
// Breaks the Chain.
func (c Chain[T]) Some(predicate func(T) bool) bool {
return u.Some(c.Value, predicate)
}

View file

@ -53,7 +53,7 @@ func TestChainFilterMapEach(t *testing.T) {
assert.Equal(t, want, res) assert.Equal(t, want, res)
} }
func TestChainFilterMapEvery(t *testing.T) { func TestChainFilterMapAll(t *testing.T) {
assert.True(t, chain.Of(nums). assert.True(t, chain.Of(nums).
Filter(isEven). Filter(isEven).
Map(toSquare). Map(toSquare).
@ -97,11 +97,11 @@ func TestChainFilterMapPartition(t *testing.T) {
assert.Equal(t, wantRight, right) assert.Equal(t, wantRight, right)
} }
func TestChainFilterMapSome(t *testing.T) { func TestChainFilterMapAny(t *testing.T) {
assert.True(t, chain.Of(nums). assert.True(t, chain.Of(nums).
Filter(isEven). Filter(isEven).
Map(toSquare). Map(toSquare).
Some(func(n int) bool { return n%64 == 0 })) Any(func(n int) bool { return n%64 == 0 }))
} }
var nums = []int{1, 2, 3, 4, 5, 6, 7, 8, 9} var nums = []int{1, 2, 3, 4, 5, 6, 7, 8, 9}

View file

@ -3,12 +3,12 @@ title: "Chaining"
date: 2021-12-31T13:11:41-04:00 date: 2021-12-31T13:11:41-04:00
--- ---
## NewChain ## Of
Calling `NewChain` will cause all future method calls to return wrapped objects. When you've finished the computation, Calling `Of` 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`, `All`, `Some`, will break the chain and return `Value` instantly. Methods not returning a collection such as `Reduce`, `All`, `Any`, will break the chain and return `Value` instantly.
```go ```go
package main package main

View file

@ -2,7 +2,7 @@
title: "Collections" title: "Collections"
date: 2021-12-30T13:24:39-04:00 date: 2021-12-30T13:24:39-04:00
--- ---
## All ## All
`All` 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.\
@ -23,6 +23,26 @@ func main() {
} }
``` ```
## Any
`Any` returns true if any of the values in the slice pass the predicate truth test. Short-circuits and stops traversing
the slice if a true element is found.
```go
package main
import (
"fmt"
u "github.com/rjNemo/underscore"
)
func main() {
nums := []int{1, 2, 4, 6, 8}
isEven := func(n int) bool { return n%2 == 0 }
fmt.Println(u.Any(nums, isEven)) // 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.
@ -209,23 +229,3 @@ func main() {
fmt.Println(u.Reduce(nums, sum, 0)) // 45 fmt.Println(u.Reduce(nums, sum, 0)) // 45
} }
``` ```
## Some
`Some` returns true if any of the values in the slice pass the predicate truth test. Short-circuits and stops traversing
the slice if a true element is found.
```go
package main
import (
"fmt"
u "github.com/rjNemo/underscore"
)
func main() {
nums := []int{1, 2, 4, 6, 8}
isEven := func(n int) bool { return n%2 == 0 }
fmt.Println(u.Some(nums, isEven)) // true
}
```