-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(join): implements Join methods for interface & primitives
Signed-off-by: Alexandre Nicolaie <[email protected]>
- Loading branch information
Showing
10 changed files
with
600 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package funk | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
type JoinFnc func(lx, rx reflect.Value) reflect.Value | ||
|
||
// Join combines two collections using the given join method. | ||
func Join(larr, rarr interface{}, fnc JoinFnc) interface{} { | ||
if !IsCollection(larr) { | ||
panic("First parameter must be a collection") | ||
} | ||
if !IsCollection(rarr) { | ||
panic("Second parameter must be a collection") | ||
} | ||
|
||
lvalue := reflect.ValueOf(larr) | ||
rvalue := reflect.ValueOf(rarr) | ||
if NotEqual(lvalue.Type(), rvalue.Type()) { | ||
panic("Parameters must have the same type") | ||
} | ||
|
||
return fnc(lvalue, rvalue).Interface() | ||
} | ||
|
||
// InnerJoin finds and returns matching data from two collections. | ||
func InnerJoin(lx, rx reflect.Value) reflect.Value { | ||
result := reflect.MakeSlice(reflect.SliceOf(lx.Type().Elem()), 0, lx.Len()+rx.Len()) | ||
rhash := hashSlice(rx) | ||
lhash := make(map[interface{}]struct{}, lx.Len()) | ||
|
||
for i := 0; i < lx.Len(); i++ { | ||
v := lx.Index(i) | ||
_, ok := rhash[v.Interface()] | ||
_, alreadyExists := lhash[v.Interface()] | ||
if ok && !alreadyExists { | ||
lhash[v.Interface()] = struct{}{} | ||
result = reflect.Append(result, v) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
// OuterJoin finds and returns dissimilar data from two collections. | ||
func OuterJoin(lx, rx reflect.Value) reflect.Value { | ||
ljoin := LeftJoin(lx, rx) | ||
rjoin := RightJoin(lx, rx) | ||
|
||
result := reflect.MakeSlice(reflect.SliceOf(lx.Type().Elem()), ljoin.Len()+rjoin.Len(), ljoin.Len()+rjoin.Len()) | ||
for i := 0; i < ljoin.Len(); i++ { | ||
result.Index(i).Set(ljoin.Index(i)) | ||
} | ||
for i := 0; i < rjoin.Len(); i++ { | ||
result.Index(ljoin.Len() + i).Set(rjoin.Index(i)) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// LeftJoin finds and returns dissimilar data from the first collection (left). | ||
func LeftJoin(lx, rx reflect.Value) reflect.Value { | ||
result := reflect.MakeSlice(reflect.SliceOf(lx.Type().Elem()), 0, lx.Len()) | ||
rhash := hashSlice(rx) | ||
|
||
for i := 0; i < lx.Len(); i++ { | ||
v := lx.Index(i) | ||
_, ok := rhash[v.Interface()] | ||
if !ok { | ||
result = reflect.Append(result, v) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
// LeftJoin finds and returns dissimilar data from the second collection (right). | ||
func RightJoin(lx, rx reflect.Value) reflect.Value { return LeftJoin(rx, lx) } | ||
|
||
func hashSlice(arr reflect.Value) map[interface{}]struct{} { | ||
hash := map[interface{}]struct{}{} | ||
for i := 0; i < arr.Len(); i++ { | ||
v := arr.Index(i).Interface() | ||
hash[v] = struct{}{} | ||
} | ||
return hash | ||
} |
Oops, something went wrong.