Skip to content

Commit

Permalink
feat(logic): add functional functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Apr 13, 2023
1 parent 0da3566 commit 11f1738
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions x/logic/util/func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package util

import "github.com/samber/lo"

// WhitelistBlacklistMatches returns a function that matches the given item according to the given whitelist and
// blacklist returning true if the item matches the whitelist and does not match the blacklist, and false otherwise.
// Note that if the whitelist is empty, the item is considered to match the whitelist.
func WhitelistBlacklistMatches[T any](whitelist []T, blacklist []T, predicate func(item T) func(b T) bool) func(T) bool {
return func(item T) bool {
matches := predicate(item)
return ((len(whitelist) == 0) || lo.ContainsBy(whitelist, matches)) && !lo.ContainsBy(blacklist, matches)
}
}

// Indexed returns a function that applies the given function to the given item and returns the result.
// It's a convenience function to be used with lo.Map which transforms a predicate function into a mapper function
// by adding an index argument (which is ignored).
func Indexed[T any, U any](f func(t T) U) func(T, int) U {
return func(t T, _ int) U {
return f(t)
}
}

0 comments on commit 11f1738

Please sign in to comment.