Skip to content

Commit 3254597

Browse files
authored
Merge pull request #30 from versecafe/fix-cartesian-product-types
Allow cartesian product of (a, b) instead of only (a, a)
2 parents 2ac9811 + b8af54a commit 3254597

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/gleam_community/maths/combinatorics.gleam

+3-3
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,10 @@ fn do_list_permutation_with_repetitions(
652652
/// </a>
653653
/// </div>
654654
///
655-
pub fn cartesian_product(xset: set.Set(a), yset: set.Set(a)) -> set.Set(#(a, a)) {
655+
pub fn cartesian_product(xset: set.Set(a), yset: set.Set(b)) -> set.Set(#(a, b)) {
656656
xset
657-
|> set.fold(set.new(), fn(accumulator0: set.Set(#(a, a)), member0: a) {
658-
set.fold(yset, accumulator0, fn(accumulator1: set.Set(#(a, a)), member1: a) {
657+
|> set.fold(set.new(), fn(accumulator0: set.Set(#(a, b)), member0: a) {
658+
set.fold(yset, accumulator0, fn(accumulator1: set.Set(#(a, b)), member1: b) {
659659
set.insert(accumulator1, #(member0, member1))
660660
})
661661
})

test/gleam_community/maths/combinatorics_test.gleam

+8-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn int_combination_test() {
7272

7373
combinatorics.combination(7, 5, option.Some(combinatorics.WithRepetitions))
7474
|> should.equal(Ok(462))
75-
// NOTE: Tests with the 'combination' function that produce values that exceed
75+
// NOTE: Tests with the 'combination' function that produce values that exceed
7676
// precision of the JavaScript 'Number' primitive will result in errors
7777
}
7878

@@ -697,4 +697,11 @@ pub fn example_test() {
697697
|> should.equal(
698698
set.from_list([#(1.0, 1.0), #(1.0, 2.0), #(10.0, 1.0), #(10.0, 2.0)]),
699699
)
700+
701+
// Cartesian product of two sets with different types
702+
set.from_list(["1", "10"])
703+
|> combinatorics.cartesian_product(set.from_list([1.0, 2.0]))
704+
|> should.equal(
705+
set.from_list([#("1", 1.0), #("1", 2.0), #("10", 1.0), #("10", 2.0)]),
706+
)
700707
}

0 commit comments

Comments
 (0)