我需要一些帮助来修改(简化)我的代码.
这是一个例子:
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)
})
}
Run Code Online (Sandbox Code Playgroud)
我需要更改flatMap方法并避免使用"ELSE"部分代码的情况
据我所知,我们可以使用部分函数或[Option].如果我们的"if"语句为FALSE,它可以帮助我们避免情况.如果我从代码中删除ELSE部分,scala不会给我机会编译我的代码.谢谢!