From 81c98e8bf5cb21b8be911a7a85a51e674f2b847e Mon Sep 17 00:00:00 2001 From: Ruidy Date: Mon, 3 Jan 2022 11:20:36 -0400 Subject: [PATCH] refactor: rename `Some` to `Any` --- README.md | 4 +-- some.go => any.go | 4 +-- some_test.go => any_test.go | 4 +-- chain/chain.go | 30 +++++++++++------------ chain/chain_test.go | 6 ++--- docs/content/docs/chaining.md | 6 ++--- docs/content/docs/collections.md | 42 ++++++++++++++++---------------- 7 files changed, 48 insertions(+), 48 deletions(-) rename some.go => any.go (55%) rename some_test.go => any_test.go (82%) diff --git a/README.md b/README.md index bcc06b6..fd35abe 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ go test ./... ### Collections - `All` +- `Any` - `Contains` (only numerics values at the moment) - `Each` - `Filter` @@ -96,14 +97,13 @@ go test ./... - `Min` - `Partition` - `Reduce` -- `Some` ### Chaining 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. -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 diff --git a/some.go b/any.go similarity index 55% rename from some.go rename to any.go index ab1b869..e74934b 100644 --- a/some.go +++ b/any.go @@ -1,8 +1,8 @@ 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. -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 { if predicate(v) { return true diff --git a/some_test.go b/any_test.go similarity index 82% rename from some_test.go rename to any_test.go index 05d4395..20206f2 100644 --- a/some_test.go +++ b/any_test.go @@ -12,12 +12,12 @@ func TestSome(t *testing.T) { nums := []int{1, 2, 4, 6, 8} 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) { nums := []int{2, 4, 6, 8} isOdd := func(n int) bool { return n%2 != 0 } - assert.False(t, u.Some(nums, isOdd)) + assert.False(t, u.Any(nums, isOdd)) } diff --git a/chain/chain.go b/chain/chain.go index 2bf7ee3..3e81489 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -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, // 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] { 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. func (c Chain[T]) Contains(value T) bool { return u.Contains(c.Value, value) @@ -29,13 +43,6 @@ func (c Chain[T]) Each(action func(T)) { 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). func (c Chain[T]) Filter(predicate func(n T) bool) Chain[T] { 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 { 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) -} diff --git a/chain/chain_test.go b/chain/chain_test.go index 106a46b..f357922 100644 --- a/chain/chain_test.go +++ b/chain/chain_test.go @@ -53,7 +53,7 @@ func TestChainFilterMapEach(t *testing.T) { assert.Equal(t, want, res) } -func TestChainFilterMapEvery(t *testing.T) { +func TestChainFilterMapAll(t *testing.T) { assert.True(t, chain.Of(nums). Filter(isEven). Map(toSquare). @@ -97,11 +97,11 @@ func TestChainFilterMapPartition(t *testing.T) { assert.Equal(t, wantRight, right) } -func TestChainFilterMapSome(t *testing.T) { +func TestChainFilterMapAny(t *testing.T) { assert.True(t, chain.Of(nums). Filter(isEven). 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} diff --git a/docs/content/docs/chaining.md b/docs/content/docs/chaining.md index 0fa9bb1..d5619c6 100644 --- a/docs/content/docs/chaining.md +++ b/docs/content/docs/chaining.md @@ -3,12 +3,12 @@ title: "Chaining" 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. -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 package main diff --git a/docs/content/docs/collections.md b/docs/content/docs/collections.md index 47dd4f1..03d0fa3 100644 --- a/docs/content/docs/collections.md +++ b/docs/content/docs/collections.md @@ -2,7 +2,7 @@ title: "Collections" date: 2021-12-30T13:24:39-04:00 --- - + ## All `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` returns true if the value is present in the slice. @@ -209,23 +229,3 @@ func main() { 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 -} -``` \ No newline at end of file