-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
collections/src/main/java/com/crazylegend/collections/CollectionsExtensions5.kt
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,26 @@ | ||
package com.crazylegend.collections | ||
import kotlin.collections.associateBy | ||
|
||
/** | ||
* Created by Hristijan, date 10/5/21 | ||
*/ | ||
fun <FIRST_LIST_TYPE, SECOND_LIST_TYPE, FIRST_LIST_TRANSFORMER, SECOND_LIST_TRANSFORMER, RETURN_TYPE> | ||
Iterable<FIRST_LIST_TYPE>.zipBy( | ||
other: Iterable<SECOND_LIST_TYPE>, | ||
firstListTransformer: (FIRST_LIST_TYPE) -> FIRST_LIST_TRANSFORMER, | ||
secondListTransformer: (SECOND_LIST_TYPE) -> SECOND_LIST_TRANSFORMER, | ||
pairPredicate: (first: FIRST_LIST_TYPE?, second: SECOND_LIST_TYPE?) -> RETURN_TYPE? | ||
): List<RETURN_TYPE> { | ||
val firstByKey: Map<FIRST_LIST_TRANSFORMER, FIRST_LIST_TYPE> = associateBy { | ||
firstListTransformer(it) | ||
} | ||
val secondByKey: Map<SECOND_LIST_TRANSFORMER, SECOND_LIST_TYPE> = other.associateBy { | ||
secondListTransformer(it) | ||
} | ||
val resultList = (firstByKey.keys + secondByKey.keys).map { (firstByKey[it]) to (secondByKey[it]) } | ||
return resultList.asSequence().mapNotNull { | ||
val firstPair = it.first | ||
val secondPair = it.second | ||
pairPredicate(firstPair, secondPair) | ||
}.toList() | ||
} |