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

Without By #511

Closed
wants to merge 2 commits into from
Closed

Without By #511

wants to merge 2 commits into from

Conversation

usman1100
Copy link
Contributor

@usman1100 usman1100 commented Aug 11, 2024

Without By

Resolves #505

Description

The WithoutBy function filters a slice of elements based on a given predicate function. It returns a new slice containing only the elements for which the predicate function returns false.

Examples

Filtering unverified users

func isValidEmail(email string) bool {
        // Basic email validation using regular expression
        emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$`)
        return emailRegex.MatchString(email)
}

func cleanUserList(users []User) []User {
        return WithoutBy(users, func(u User) bool { return !isValidEmail(u.Email) })
}

Filtering even numbers

func isEven(num int) bool {
        return num%2 == 0
}

func removeEven(numbers []int) []int {
        return WithoutBy(numbers, isEven)
}

@samber
Copy link
Owner

samber commented Aug 11, 2024

I think you are implementing the lo.Reject helper.

@usman1100 usman1100 closed this Aug 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: Add SliceToSet, WithoutBy
2 participants