mirror of
https://github.com/twitter/the-algorithm.git
synced 2024-12-23 02:31:52 +01:00
Change Refactor
inverted if and changed for comprehension to a foldRight with a match on the result
This commit is contained in:
parent
ec83d01dca
commit
573fab9526
@ -102,31 +102,29 @@ object KnownForSources {
|
|||||||
TypedPipe
|
TypedPipe
|
||||||
.from(TextLine(textFile))
|
.from(TextLine(textFile))
|
||||||
.flatMap { str =>
|
.flatMap { str =>
|
||||||
if (!str.startsWith("#")) {
|
if (str.startsWith("#")) {none}
|
||||||
try {
|
try {
|
||||||
val tokens = str.trim.split("\\s+")
|
val tokens = str.trim.split("\\s+")
|
||||||
val res = Array.newBuilder[(Int, Float)]
|
val userId = tokens(0).toLong
|
||||||
val userId = tokens(0).toLong
|
(1 until tokens.length).foldRight(Array.newBuilder[(Int, Float)])((i, r) => {
|
||||||
for (i <- 1 until tokens.length) {
|
val Array(cIdStr, scoreStr) = tokens(i).split(":")
|
||||||
val Array(cIdStr, scoreStr) = tokens(i).split(":")
|
val clusterId = cIdStr.toInt
|
||||||
val clusterId = cIdStr.toInt
|
val score = scoreStr.toFloat
|
||||||
val score = scoreStr.toFloat
|
val newEntry = (clusterId, score)
|
||||||
val newEntry = (clusterId, score)
|
r += newEntry
|
||||||
res += newEntry
|
}).result() match {
|
||||||
}
|
case (res) if res.nonEmpty => Some((userId, res.result()))
|
||||||
val result = res.result
|
_ => none
|
||||||
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
|
|
||||||
}
|
}
|
||||||
} else None
|
}
|
||||||
|
catch {
|
||||||
|
case ex: Throwable =>
|
||||||
|
log.warning(
|
||||||
|
s"Error while loading knownFor from $textFile for line <$str>: " +
|
||||||
|
ex.getMessage
|
||||||
|
)
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user