Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1/2] custodian: look up proofs in local universe as well #726

Merged
merged 11 commits into from
Jan 12, 2024
44 changes: 44 additions & 0 deletions fn/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ func All[T any](xs []T, pred func(T) bool) bool {
return true
}

// AllMapItems returns true if the passed predicate returns true for all items
// in the map.
func AllMapItems[T any, K comparable](xs map[K]T, pred func(T) bool) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Looking forward to when/if they add more direct iterator stuff to the Go stdlib, then we'd only need one version of this.

for i := range xs {
if !pred(xs[i]) {
return false
}
}

return true
}

// Any returns true if the passed predicate returns true for any item in the
// slice.
func Any[T any](xs []T, pred func(T) bool) bool {
Expand All @@ -142,12 +154,30 @@ func Any[T any](xs []T, pred func(T) bool) bool {
return false
}

// AnyMapItem returns true if the passed predicate returns true for any item in
// the map.
func AnyMapItem[T any, K comparable](xs map[K]T, pred func(T) bool) bool {
for i := range xs {
if pred(xs[i]) {
return true
}
}

return false
}

// NotAny returns true if the passed predicate returns false for all items in
// the slice.
func NotAny[T any](xs []T, pred func(T) bool) bool {
return !Any(xs, pred)
}

// NotAnyMapItem returns true if the passed predicate returns false for all
// items in the map.
func NotAnyMapItem[T any, K comparable](xs map[K]T, pred func(T) bool) bool {
return !AnyMapItem(xs, pred)
}

// Count returns the number of items in the slice that match the predicate.
func Count[T any](xs []T, pred func(T) bool) int {
var count int
Expand All @@ -161,6 +191,20 @@ func Count[T any](xs []T, pred func(T) bool) int {
return count
}

// CountMapItems returns the number of items in the map that match the
// predicate.
func CountMapItems[T any, K comparable](xs map[K]T, pred func(T) bool) int {
var count int

for i := range xs {
if pred(xs[i]) {
count++
}
}

return count
}

// First returns the first item in the slice that matches the predicate, or an
// error if none matches.
func First[T any](xs []*T, pred func(*T) bool) (*T, error) {
Expand Down
28 changes: 21 additions & 7 deletions fn/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ package fn
// This function can be used instead of the normal range loop to ensure that a
// loop scoping bug isn't introduced.
func ForEachErr[T any](s []T, f func(T) error) error {
for _, item := range s {
item := item

if err := f(item); err != nil {
for i := range s {
if err := f(s[i]); err != nil {
return err
}
}
Expand All @@ -22,9 +20,17 @@ func ForEachErr[T any](s []T, f func(T) error) error {
// This can be used to ensure that any normal for-loop don't run into bugs due
// to loop variable scoping.
func ForEach[T any](items []T, f func(T)) {
for _, item := range items {
item := item
f(item)
for i := range items {
f(items[i])
}
}

// ForEachMapItem is a generic implementation of a for-each (map with side
// effects). This can be used to ensure that any normal for-loop don't run into
// bugs due to loop variable scoping.
jharveyb marked this conversation as resolved.
Show resolved Hide resolved
func ForEachMapItem[T any, K comparable](items map[K]T, f func(T)) {
for i := range items {
f(items[i])
}
}

Expand All @@ -38,6 +44,14 @@ func Enumerate[T any](items []T, f func(int, T)) {
}
}

// EnumerateMap is a generic enumeration function. The closure will be called
// for each key and item in the passed-in map.
func EnumerateMap[T any, K comparable](items map[K]T, f func(K, T)) {
for key := range items {
f(key, items[key])
}
}

// MakeSlice is a generic function shorthand for making a slice out of a set
// of elements. This can be used to avoid having to specify the type of the
// slice as well as the types of the elements.
Expand Down