Merge pull request #30 from versecafe/fix-cartesian-product-types

Allow cartesian product of (a, b) instead of only (a, a)
This commit is contained in:
Nicklas Sindlev Andersen 2024-09-28 18:23:30 +02:00 committed by GitHub
commit 3254597cef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 4 deletions

View file

@ -652,10 +652,10 @@ fn do_list_permutation_with_repetitions(
/// </a>
/// </div>
///
pub fn cartesian_product(xset: set.Set(a), yset: set.Set(a)) -> set.Set(#(a, a)) {
pub fn cartesian_product(xset: set.Set(a), yset: set.Set(b)) -> set.Set(#(a, b)) {
xset
|> set.fold(set.new(), fn(accumulator0: set.Set(#(a, a)), member0: a) {
set.fold(yset, accumulator0, fn(accumulator1: set.Set(#(a, a)), member1: a) {
|> set.fold(set.new(), fn(accumulator0: set.Set(#(a, b)), member0: a) {
set.fold(yset, accumulator0, fn(accumulator1: set.Set(#(a, b)), member1: b) {
set.insert(accumulator1, #(member0, member1))
})
})

View file

@ -697,4 +697,11 @@ pub fn example_test() {
|> should.equal(
set.from_list([#(1.0, 1.0), #(1.0, 2.0), #(10.0, 1.0), #(10.0, 2.0)]),
)
// Cartesian product of two sets with different types
set.from_list(["1", "10"])
|> combinatorics.cartesian_product(set.from_list([1.0, 2.0]))
|> should.equal(
set.from_list([#("1", 1.0), #("1", 2.0), #("10", 1.0), #("10", 2.0)]),
)
}