Skip to content

Commit bae5703

Browse files
committed
Generalize combinations() to Iterable
This makes it applicable to more collections such as ranges.
1 parent a255ad0 commit bae5703

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/main/kotlin/de/ronny_h/aoc/extensions/Combinations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import java.lang.Math.pow
66
/**
77
* @return All pairwise combinations of list elements without the reflexive ones.
88
*/
9-
fun <E> List<E>.combinations() = sequence {
9+
fun <E> Iterable<E>.combinations() = sequence {
1010
forEachIndexed { i, a ->
1111
forEachIndexed { j, b ->
1212
if (i != j) {

src/test/kotlin/de/ronny_h/aoc/extensions/CombinationsTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ class CombinationsTest : StringSpec({
3232
)
3333
}
3434

35+
"a range with three elements yields all six combinations" {
36+
(1..3).combinations().toList() shouldBe
37+
listOf(
38+
1 to 2,
39+
1 to 3,
40+
2 to 1,
41+
2 to 3,
42+
3 to 1,
43+
3 to 2
44+
)
45+
}
46+
3547
"a list with non-unique elements yields non-unique combinations but still skips the identical ones" {
3648
listOf(1, 1, 2).combinations().toList() shouldBe
3749
listOf(

0 commit comments

Comments
 (0)