#973·lo

Is there a function to find if one or more elements of a slice matches one or more elements of another slice using func?

Author: TLINDENCreated Aug 13, 2026Updated Aug 24, 2026

I have the following 2 slices:

go
search := []string{"alpha", "beta", "theta"}
ref := []string{"ph", "he"}

Now I want to check if one or more elements of search contains at least one element of ref.

Currently I am using a function like this to solve it:

go
func sliceContainsSliceElem(search []string, ref []string) bool {
        return len(
                lo.Filter(
                        search,
                        func(pattern string, _ int) bool {
                                return slices.ContainsFunc(
                                        ref,
                                        func(filter string) bool {
                                                return strings.Contains(pattern, filter)
                                        })
                        })) > 0
}

While this works, it's a lot of code and a little bit hard to understand.

I also wrote a generic variant:

go
func sliceContainsSliceElemG[T any, Slice ~[]T](search Slice, ref Slice, f func(value, ref T) bool) bool {
        var found bool

        for v := range slices.Values(search) {
                for r := range slices.Values(ref) {
                        ok := f(v, r)

                        if ok {
                                found = true
                                break
                        }
                }

                if found {
                        break
                }
        }

        return found
}

Both work:

go
func main() {
        search := []string{"alpha", "beta", "theta"}
        ref := []string{"pdh", "he"}

        fmt.Println(sliceContainsSliceElem(search, ref))
        fmt.Println(sliceContainsSliceElemG(search, ref, strings.Contains))
}

// true
// true

My question is, does lo include a function like this (I haven't found one)? And if not, would it make sense to add one?

Thanks a lot in advance, Tom