I need some help to modify(simplify) my code.
Here an example:
def getBids(rawBids: RDD[List[String]], exchangeRates: Map[String, Double]): RDD[BidItem] = {
rawBids.filter(bidList => !bidList(2).matches(ERROR_REGEXP))
.flatMap(bidList => {
Constants.TARGET_LOSAS.map { losa =>
val indexOfLosa = Constants.BIDS_HEADER.indexOf(losa)
if (bidList(indexOfLosa) != null && !bidList(indexOfLosa).equals("") && isDoubleNumber(bidList(indexOfLosa))) {
BidItem(bidList.head, bidList(1), losa, bidList(indexOfLosa).toDouble)
} else {
BidItem(bidList.head, bidList(1), losa, 0)
}
}
})
.filter(unit => unit.price != 0)
.map(unit => {
val convertedPrice = usdToEur(unit.price, unit.bidDate, exchangeRates)
val convertedDate = changeDateFormat(unit.bidDate)
BidItem(unit.motelId, convertedDate, unit.loSa, convertedPrice)
})
}
I need to change flatMap method and avoid situation with "ELSE" part of code
As far as I know, we can use partial functions or [Option]. It can help us to avoid situation if our "if" statement is FALSE. If I remove ELSE part from code, scala don't give me a chance to compile my code. Thanks!
val doubleValue = Option(bidList(indexOfLosa))
.filterNot(_.equals(""))
.filter(isDoubleNumber)
.map(_.toDouble)
.getOrElse(0)
BidItem(bidList.head, bidList(1), losa, doubleValue)