From 54a77fa1a2211eb01f7af7d013a429f2cc9af540 Mon Sep 17 00:00:00 2001 From: twentylemon Date: Tue, 24 Dec 2024 23:48:56 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20`cartesianProduct`;=20combine?= =?UTF-8?q?=20`combinations`=20file=20into=20`collections`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/lemon/advent/lib/collections.scala | 36 +++++++++++++++++++ .../org/lemon/advent/lib/combinations.scala | 28 --------------- 2 files changed, 36 insertions(+), 28 deletions(-) delete mode 100644 src/main/scala/org/lemon/advent/lib/combinations.scala diff --git a/src/main/scala/org/lemon/advent/lib/collections.scala b/src/main/scala/org/lemon/advent/lib/collections.scala index 66d88d0..3091462 100644 --- a/src/main/scala/org/lemon/advent/lib/collections.scala +++ b/src/main/scala/org/lemon/advent/lib/collections.scala @@ -15,3 +15,39 @@ extension [A](it: Iterator[A]) * @return the nth element of the iterator, or None if there is no nth element */ def nthOption(n: Int): Option[A] = it.drop(n).nextOption + +extension [A](it: Iterable[A]) + /** Returns an iterator view of the Cartesian product of this iterable and another. + * Also known as the cross product + * + * @param rhs the other iterable + * @return all pairs of elements (a, b) where a is from this iterable and b is from the other iterable + */ + def cartesianProduct[B](rhs: Iterable[B]): Iterator[(A, B)] = + for a <- it.iterator; b <- rhs.iterator yield (a, b) + + /** Returns an iterator of all unordered pairs of elements in the iterable. + * If the collection contains duplicates, the pairs will not be unique. + * + * @return iterator of all unordered pairs + */ + def pairs: Iterator[(A, A)] = + for + (x, i) <- it.iterator.zipWithIndex + (y, j) <- it.iterator.zipWithIndex + if i < j + yield (x, y) + + /** Returns an iterator of all unordered triples of elements in the iterable. + * If the collection contains duplicates, the triples will not be unique. + * + * @return iterator of all unordered triples + */ + def triples: Iterator[(A, A, A)] = + for + (x, i) <- it.iterator.zipWithIndex + (y, j) <- it.iterator.zipWithIndex + (z, k) <- it.iterator.zipWithIndex + if i < j + if j < k + yield (x, y, z) diff --git a/src/main/scala/org/lemon/advent/lib/combinations.scala b/src/main/scala/org/lemon/advent/lib/combinations.scala deleted file mode 100644 index e95942c..0000000 --- a/src/main/scala/org/lemon/advent/lib/combinations.scala +++ /dev/null @@ -1,28 +0,0 @@ -package org.lemon.advent.lib - -extension [T](xs: Iterable[T]) - /** Returns an iterator of all unordered pairs of elements in the iterable. - * If the collection contains duplicates, the pairs will not be unique. - * - * @return iterator of all unordered pairs - */ - def pairs: Iterator[(T, T)] = - for - (x, i) <- xs.iterator.zipWithIndex - (y, j) <- xs.iterator.zipWithIndex - if i < j - yield (x, y) - - /** Returns an iterator of all unordered triples of elements in the iterable. - * If the collection contains duplicates, the triples will not be unique. - * - * @return iterator of all unordered triples - */ - def triples: Iterator[(T, T, T)] = - for - (x, i) <- xs.iterator.zipWithIndex - (y, j) <- xs.iterator.zipWithIndex - (z, k) <- xs.iterator.zipWithIndex - if i < j - if j < k - yield (x, y, z)