-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dea3ca
commit 0c88071
Showing
2 changed files
with
195 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,81 @@ | ||
// Package check has functions to check if some condition is valid for slices and maps | ||
// TODO indexed | ||
// TODO all | ||
package check | ||
|
||
// All checks that the provided slice has every element satifying the checker function | ||
func All[T any](s []T, f func(T) bool) bool { | ||
return len(s) != 0 && !hasAny[T](false)(s, f) | ||
} | ||
|
||
// Some checks that the provided slice has any element that satisfies the checker function | ||
func Some[T any](s []T, f func(T) bool) bool { | ||
return check[T](true)(s, f) | ||
return len(s) != 0 && hasAny[T](true)(s, f) | ||
} | ||
|
||
// None checks that the provided slice has no element that satisfies the checker function | ||
func None[T any](s []T, f func(T) bool) bool { | ||
return check[T](false)(s, f) | ||
return !hasAny[T](true)(s, f) | ||
} | ||
|
||
// All checks that the provided map has every key satifying the checker function | ||
func AllKeys[T comparable, U any](m map[T]U, f func(T) bool) bool { | ||
return len(m) != 0 && !hasAnyKey[T, U](false)(m, f) | ||
} | ||
|
||
// SomeKey checks that the provided map has any key that satisfies the checker function | ||
func SomeKey[T comparable, U any](m map[T]U, f func(T) bool) bool { | ||
return checkKey[T, U](true)(m, f) | ||
return hasAnyKey[T, U](true)(m, f) | ||
} | ||
|
||
// NoKey checks that the provided map has no key that satisfies the checker function | ||
func NoKey[T comparable, U any](m map[T]U, f func(T) bool) bool { | ||
return checkKey[T, U](false)(m, f) | ||
return !hasAnyKey[T, U](true)(m, f) | ||
} | ||
|
||
// All checks that the provided map has every value satifying the checker function | ||
func AllValues[T comparable, U any](m map[T]U, f func(U) bool) bool { | ||
return len(m) != 0 && !hasAnyVal[T, U](false)(m, f) | ||
} | ||
|
||
// SomeValue checks that the provided map has any value that satisfies the checker function | ||
func SomeValue[T comparable, U any](m map[T]U, f func(U) bool) bool { | ||
return checkVal[T, U](true)(m, f) | ||
return hasAnyVal[T, U](true)(m, f) | ||
} | ||
|
||
// NoValue checks that the provided map has no value that satisfies the checker function | ||
func NoValue[T comparable, U any](m map[T]U, f func(U) bool) bool { | ||
return checkVal[T, U](false)(m, f) | ||
return !hasAnyVal[T, U](true)(m, f) | ||
} | ||
|
||
func check[T any](b bool) func(s []T, f func(T) bool) bool { | ||
func hasAny[T any](b bool) func(s []T, f func(T) bool) bool { | ||
return func(s []T, f func(T) bool) bool { | ||
for _, v := range s { | ||
if f(v) { | ||
return b | ||
if f(v) == b { | ||
return true | ||
} | ||
} | ||
return !b | ||
return false | ||
} | ||
} | ||
func checkVal[T comparable, U any](b bool) func(m map[T]U, f func(U) bool) bool { | ||
|
||
func hasAnyVal[T comparable, U any](b bool) func(m map[T]U, f func(U) bool) bool { | ||
return func(m map[T]U, f func(U) bool) bool { | ||
for _, v := range m { | ||
if f(v) { | ||
return b | ||
if f(v) == b { | ||
return true | ||
} | ||
} | ||
return !b | ||
return false | ||
} | ||
} | ||
|
||
func checkKey[T comparable, U any](b bool) func(m map[T]U, f func(T) bool) bool { | ||
func hasAnyKey[T comparable, U any](b bool) func(m map[T]U, f func(T) bool) bool { | ||
return func(m map[T]U, f func(T) bool) bool { | ||
for k := range m { | ||
if f(k) { | ||
return b | ||
if f(k) == b { | ||
return true | ||
} | ||
} | ||
return !b | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters