elliotchance / pie

🍕 Enjoy a slice! A utility library for dealing with slices and maps that focuses on type safety and performance.
https://pkg.go.dev/github.com/elliotchance/pie/v2
MIT License
1.96k stars 91 forks source link

IntersectUsing() new function #161

Closed AlessandroLorenzi closed 4 years ago

AlessandroLorenzi commented 4 years ago

This change is Reviewable

AlessandroLorenzi commented 4 years ago

refs: #72

codecov-io commented 4 years ago

Codecov Report

Merging #161 into master will increase coverage by 0.03%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #161      +/-   ##
==========================================
+ Coverage   98.66%   98.69%   +0.03%     
==========================================
  Files           8        8              
  Lines        1792     1842      +50     
==========================================
+ Hits         1768     1818      +50     
  Misses         12       12              
  Partials       12       12
Impacted Files Coverage Δ
pie/float64s_pie.go 98.82% <100%> (+0.04%) :arrow_up:
pie/carpointers_pie.go 98.95% <100%> (+0.01%) :arrow_up:
pie/cars_pie.go 98.95% <100%> (+0.01%) :arrow_up:
pie/ints_pie.go 98.82% <100%> (+0.04%) :arrow_up:
pie/strings_pie.go 97.89% <100%> (+0.08%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 9a499cb...6c54db5. Read the comment docs.

elliotchance commented 4 years ago

Please fix the merge conflict before I review.

AlessandroLorenzi commented 4 years ago

hi @elliotchance , what you think about a solution like this?

too overcomplicated?

package functions

// IntersectUsing returns items that exist in all lists, using equals function
//
// It returns slice without any duplicates.
// If zero slice arguments are provided, then nil is returned.
func (ss SliceType) IntersectUsing(equals func(ElementType, ElementType) (bool, interface{}), slices ...SliceType) []interface{} {
    if slices == nil {
        return nil
    }

    found := map[interface{}]int{}

    for _, e1 := range ss {
        for _, s2 := range slices {
            foundInSlice := false
            for _, e2 := range s2 {
                chekFound, checkValue := equals(e1, e2)
                if chekFound {
                    found[checkValue]++
                    foundInSlice = true
                    break // if found the element don't check other elements in this slice
                }
            }
            if !foundInSlice {
                break // if not found in this slice don't check other slices
            }
        }
    }
    ss2 := []interface{}{}

    for value, count := range found {
        if count == len(slices) {
            ss2 = append(ss2, value)
        }
    }
    return ss2
}
AlessandroLorenzi commented 4 years ago

I'll restart this function from zero