From dbb0a4b263bc7ac8bcd80151a4c8b122de199da0 Mon Sep 17 00:00:00 2001 From: denon1 Date: Sat, 1 Apr 2023 11:43:44 +0200 Subject: [PATCH] Change uses fold over foreach now --- .../EigenVectorsForSparseSymmetric.scala | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/scala/com/twitter/simclusters_v2/scalding/EigenVectorsForSparseSymmetric.scala b/src/scala/com/twitter/simclusters_v2/scalding/EigenVectorsForSparseSymmetric.scala index 7171e0e7a..98313ba4a 100644 --- a/src/scala/com/twitter/simclusters_v2/scalding/EigenVectorsForSparseSymmetric.scala +++ b/src/scala/com/twitter/simclusters_v2/scalding/EigenVectorsForSparseSymmetric.scala @@ -44,22 +44,17 @@ object EigenVectorsForSparseSymmetric { * @return the constructed matrix */ def getMatrix(nonzeros: Iterable[(Int, Int, Double)], nRows: Int, nCols: Int): Matrix = { - val matrix = new LinkedSparseMatrix(nRows, nCols) - var numEntries = 0 - var maxRow = 0 - var maxCol = 0 - - nonzeros.foreach { - case (i, j, v) => - if (i > maxRow) { - maxRow = i - } - if (j > maxCol) { - maxCol = j - } - numEntries += 1 - matrix.set(i, j, v) + val baseCase = (new LinkedSparseMatrix(nRows, nCols), 0, 0, 0) + val partialNonzeros = nonzeros.foldRight(baseCase) + val (matrix, numEntries, maxRow, maxCol) = partialNonzeros{ + case ((i, j, v), (matrix, numEntries, maxRow, maxCol) ) => (i, j) match { + case (_, _) if (i > maxRow) && (j > maxCol) => (matrix.set(i, j, v), (numEntries += 1, i, j)) + case (_, _) if (i > maxRow) => (matrix.set(i, j, v), (numEntries += 1, i, maxCol)) + case (_, _) if (j > maxCol) => (matrix.set(i, j, v), (numEntries += 1, maxRow, j)) + case (_, _) => (matrix.set(i, j, v), (numEntries += 1, maxRow, maxCol)) + } } + log.info( "Finished building matrix with %d entries and maxRow %d and maxCol %d" .format(numEntries, maxRow, maxCol))