Skip to content

Instantly share code, notes, and snippets.

@kjkrol
Created November 30, 2020 17:04
Show Gist options
  • Save kjkrol/06de30a26bf24ee30262c266b0c96da8 to your computer and use it in GitHub Desktop.
Save kjkrol/06de30a26bf24ee30262c266b0c96da8 to your computer and use it in GitHub Desktop.
Inner Join
inline fun <T : Any, U : Any> Collection<T>.innerJoin(
with: Collection<U>,
on: (Pair<T, U>) -> Boolean
): Collection<Pair<T, U>> = map { t ->
val intersection = with.filter { on(Pair(t, it)) }
Pair(t, intersection)
}
.filter { pair -> pair.second.isNotEmpty() }
.flatMap { pair -> pair.second.map { Pair(pair.first, it) } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment