Skip to content

Add cross join (#1) #5203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AlexCue987
Copy link
Contributor

add binary operation crossJoin, as it is commonly used

Comment on lines +538 to +561
/**
* Returns a list containing all possible pairs with left field from the first collection,
* and the right field from the second collection,
* applying the provided transformation against those pairs.
*
* The returned set preserves the element iteration order of both original collections.
* It begins with all the pairs with the first element of left collection,
* combined with all the elements of the right one in the order of the right collection.
*
* @sample samples.collections.Collections.BinaryOperations.crossJoinWithTransformation
*/
fun <T, U, V> crossJoin(
left: Collection<T>,
right: Collection<U>,
transformation: (left: T, right: U) -> V,
): Sequence<V> {
return sequence {
left.forEach { leftElement ->
right.forEach { rightElement ->
yield(transformation(leftElement, rightElement))
}
}
}
}
Copy link

@rlaope rlaope Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the crossJoin implementation you've provided, I believe it's possible to extend the concept to combine multiple elements from multiple collections, not just pairs of two elements. This could be achieved by creating a function that takes a list of collections and performs a multi-element cross join operation, producing combinations of elements from each collection.

The function signature could look something like this:

fun <T, V> multiCrossJoin(
    collections: List<Collection<T>>,
    transformation: (List<T>) -> V
): Sequence<V> {
    var result: Sequence<List<T>> = sequenceOf(emptyList())

    for (collection in collections) {
        result = result.flatMap { acc ->
            collection.map { element -> acc + element }
        }
    }

    return result.map { transformation(it) }
}

This function would take a list of collections and a transformation function, and it would perform a multi-element cross join operation to generate combinations of elements from each collection. The implementation would involve iterating through each collection and combining the elements to produce the desired combinations.

I believe that such a function would provide a flexible and powerful way to generate combinations from multiple collections of elements.

Copy link
Contributor Author

@AlexCue987 AlexCue987 Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the crossJoin implementation you've provided, I believe it's possible to extend the concept to combine multiple elements from multiple collections, not just pairs of two elements. This could be achieved by creating a function that takes a list of collections and performs a multi-element cross join operation, producing combinations of elements from each collection.

The function signature could look something like this:

fun <T, V> multiCrossJoin(
    collections: List<Collection<T>>,
    transformation: (List<T>) -> V
): Sequence<V> {
    var result: Sequence<List<T>> = sequenceOf(emptyList())

    for (collection in collections) {
        result = result.flatMap { acc ->
            collection.map { element -> acc + element }
        }
    }

    return result.map { transformation(it) }
}

This function would take a list of collections and a transformation function, and it would perform a multi-element cross join operation to generate combinations of elements from each collection. The implementation would involve iterating through each collection and combining the elements to produce the desired combinations.

I believe that such a function would provide a flexible and powerful way to generate combinations from multiple collections of elements.

You are solving a different problem: all elements in your collections have the same type T. The whole point of this PR is to handle pairs of elements with different types, left: T, right: U.

Cross Join

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are solving a different problem: all elements in your collections have the same type T. The whole point of this PR is to handle pairs of elements with different types, left: T, right: U.

Cross Join

Sure, I understand. It seems that I overlooked the fact that the types of "left" and "right" are different. I created a function to find all possible combinations of elements of the same type, and it looks like you implemented something different. If this PR gets merged, I will try implementing it based on this idea and submit another PR. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants