我有以下功能:Scala的功能篩選字段只數
def isAllDigits(x: String) = x forall Character.isDigit
def filterNum(x: (Int, String)) : Boolean = {
accumNum.add(1)
if(isAllDigits(x._2)) false
else true
}
我傳遞鍵/值的,我要檢查值是數值。出於某種原因,它被過濾掉:
res10: Array[(Int, String)] = Array((1,18964), (2,39612), (3,1), (4,""), (5,""), (6,""), (7,""), (8,""), (9,1), (10,""))
,但允許這樣的:
res9: Array[(Int, String)] = Array((18,1000.0), (22,23.99), (18,1001.0), (22,23.99), (18,300.0), (22,23.99), (18,300.0), (22,23.99), (18,300.0), (22,23.99))
不.isDigit只允許雙打?但是我很困惑,爲什麼當x是(Int,String)被傳入的double/int被看作是一個字符串。
編輯: 我使用這個功能在星火下列要求:
val numFilterRDD = numRDD.filter(filterNum)
numRDD.take()例如:
res11: Array[(Int, String)] = Array((1,18964), (2,39612), (3,1), (4,""), (5,""), (6,""), (7,""), (8,""), (9,1), (10,""), (11,""), (16,""), (18,1000.0), (19,""), (20,""), (21,""), (22,23.99), (23,""), (24,""), (25,""))
你能提供一些你如何使用這個filterNum函數的例子嗎? – leshkin
我不知道這是否只是一個錯字,但注意到res10的元素不是類型(Int,String),而是(Any,String)。這甚至不應該編譯。 – abalcerek
增加了額外的上下文。 – theMadKing