I'm trying to rewrite a function in a functional style. I know the function is bad. But how can I avoid the usage of the var field watermarkTime?
The function sets day "1" for the rows with a continuous increasing timestamp. If the timestamp is lesser than the timestamp of the previous row, then set day "2".
case class Row(time: String, name: String, var day: Int)
val rows = List(new Row("09:18:52", "3_0711-082757_01001", 0),
new Row("09:23:18", "3_0711-082757_01002", 0),
new Row("09:33:43", "3_0711-082757_01004", 0),
new Row("10:20:00", "3_0711-082757_01011", 0),
new Row("05:03:38", "3_0711-082757_02001", 0), // set secound day
new Row("05:07:51", "3_0711-082757_02002", 0),
new Row("05:13:02", "3_0711-082757_02003", 0),
new Row("05:19:16", "3_0711-082757_02004", 0),
new Row("10:54:27", "3_0711-082757_02015", 0), // set first day
new Row("11:00:38", "3_0711-082757_02016", 0),
new Row("11:07:28", "3_0711-082757_02017", 0))
def setDayFP(rows: List[Row]): List[Row] = {
var watermarkTime = 0
for (row <- rows) {
val newTime = row.time.replaceAll(":","").toInt
if (watermarkTime < newTime) {
watermarkTime = newTime
row.day = 1
} else {
row.day = 2
}
}
return rows
}
Here is the result (sorted by day and name):
Row(09:18:52,3_0711-082757_01001,1)
Row(09:23:18,3_0711-082757_01002,1)
Row(09:33:43,3_0711-082757_01004,1)
Row(10:20:00,3_0711-082757_01011,1)
Row(10:54:27,3_0711-082757_02015,1)
Row(11:00:38,3_0711-082757_02016,1)
Row(11:07:28,3_0711-082757_02017,1)
Row(05:03:38,3_0711-082757_02001,2)
Row(05:07:51,3_0711-082757_02002,2)
Row(05:13:02,3_0711-082757_02003,2)
Row(05:19:16,3_0711-082757_02004,2)
I'm looking for a better solution. Thanks in advance!