From 573fab9526b6720498f7ad060365bfca9afb6680 Mon Sep 17 00:00:00 2001 From: denon1 Date: Sat, 1 Apr 2023 01:52:22 +0200 Subject: [PATCH 1/3] Change Refactor inverted if and changed for comprehension to a foldRight with a match on the result --- .../scalding/KnownForSources.scala | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala b/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala index 217f521ac..f59968e78 100644 --- a/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala +++ b/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala @@ -102,31 +102,29 @@ object KnownForSources { TypedPipe .from(TextLine(textFile)) .flatMap { str => - if (!str.startsWith("#")) { - try { - val tokens = str.trim.split("\\s+") - val res = Array.newBuilder[(Int, Float)] - val userId = tokens(0).toLong - for (i <- 1 until tokens.length) { - val Array(cIdStr, scoreStr) = tokens(i).split(":") - val clusterId = cIdStr.toInt - val score = scoreStr.toFloat - val newEntry = (clusterId, score) - res += newEntry - } - val result = res.result - if (result.nonEmpty) { - Some((userId, res.result())) - } else None - } catch { - case ex: Throwable => - log.warning( - s"Error while loading knownFor from $textFile for line <$str>: " + - ex.getMessage - ) - None + if (str.startsWith("#")) {none} + try { + val tokens = str.trim.split("\\s+") + val userId = tokens(0).toLong + (1 until tokens.length).foldRight(Array.newBuilder[(Int, Float)])((i, r) => { + val Array(cIdStr, scoreStr) = tokens(i).split(":") + val clusterId = cIdStr.toInt + val score = scoreStr.toFloat + val newEntry = (clusterId, score) + r += newEntry + }).result() match { + case (res) if res.nonEmpty => Some((userId, res.result())) + _ => none } - } else None + } + catch { + case ex: Throwable => + log.warning( + s"Error while loading knownFor from $textFile for line <$str>: " + + ex.getMessage + ) + None + } } } From 376f35f08f8ee3d2020007380dc04ca30506bc9f Mon Sep 17 00:00:00 2001 From: denon1 Date: Sat, 1 Apr 2023 02:04:07 +0200 Subject: [PATCH 2/3] Change if refactored to match --- .../scalding/KnownForSources.scala | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala b/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala index f59968e78..577faf763 100644 --- a/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala +++ b/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala @@ -102,28 +102,30 @@ object KnownForSources { TypedPipe .from(TextLine(textFile)) .flatMap { str => - if (str.startsWith("#")) {none} - try { - val tokens = str.trim.split("\\s+") - val userId = tokens(0).toLong - (1 until tokens.length).foldRight(Array.newBuilder[(Int, Float)])((i, r) => { - val Array(cIdStr, scoreStr) = tokens(i).split(":") - val clusterId = cIdStr.toInt - val score = scoreStr.toFloat - val newEntry = (clusterId, score) - r += newEntry - }).result() match { - case (res) if res.nonEmpty => Some((userId, res.result())) - _ => none + str match { + case s"#$_" => none + case _ => try { + val tokens = str.trim.split("\\s+") + val userId = tokens(0).toLong + (1 until tokens.length).foldRight(Array.newBuilder[(Int, Float)])((i, r) => { + val Array(cIdStr, scoreStr) = tokens(i).split(":") + val clusterId = cIdStr.toInt + val score = scoreStr.toFloat + val newEntry = (clusterId, score) + r += newEntry + }).result() match { + case (res) if res.nonEmpty => Some((userId, res.result())) + _ => none + } + } + catch { + case ex: Throwable => + log.warning( + s"Error while loading knownFor from $textFile for line <$str>: " + + ex.getMessage + ) + None } - } - catch { - case ex: Throwable => - log.warning( - s"Error while loading knownFor from $textFile for line <$str>: " + - ex.getMessage - ) - None } } } From 645342ba2f4f6dff9b5e69cc5881a97e3b8957b4 Mon Sep 17 00:00:00 2001 From: denon1 Date: Sat, 1 Apr 2023 02:07:36 +0200 Subject: [PATCH 3/3] Fix none was Lowercase fixed for right Option Type --- .../com/twitter/simclusters_v2/scalding/KnownForSources.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala b/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala index 577faf763..5f96df6a5 100644 --- a/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala +++ b/src/scala/com/twitter/simclusters_v2/scalding/KnownForSources.scala @@ -103,7 +103,7 @@ object KnownForSources { .from(TextLine(textFile)) .flatMap { str => str match { - case s"#$_" => none + case s"#$_" => None case _ => try { val tokens = str.trim.split("\\s+") val userId = tokens(0).toLong @@ -115,7 +115,7 @@ object KnownForSources { r += newEntry }).result() match { case (res) if res.nonEmpty => Some((userId, res.result())) - _ => none + _ => None } } catch {