在scala中对行进行递归计算 [英] recursive calculation over lines in scala
本文介绍了在scala中对行进行递归计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在输入中列出了以下字符串:
I Have the following list of stings in input:
val input = List(
"17 SD1-MONT_FF_13 7(14)",
"17 QXRI1-SEDDS_13 S(01)",
"17 XFDRI1-MONDT_TT_14 7(18)",
"17 SQXI1-SSENS_14 S(01)",
"12 CRI1-MSONT_TT_15 7(18)",
"13 QSDRI1-SEDNS_15 S(01)",
"14 WSQSRI1-DEVSISE S(05)")
我对以下函数进行了编码,该函数计算列表的每一行的第三个元素上的数据类型:
I coded the following function which calclulates the datatype over the third element of each line of the list :
但是我不知道如何在每一行上递归调用此函数,因此它将数据类型添加为每一行的第4个元素,预期结果应为如下列表:
But I don't know how to call this function recursively over each line so it adds the datatype as a 4th element is each line , the expected result should be a list as follows :
val input = List(
"17 SD1-MONT_FF_13 7(14) IntegerType",
"17 QXRI1-SEDDS_13 S(01) StringType",
"17 XFDRI1-MONDT_TT_14 7(18) IntegerType",
"17 SQXI1-SSENS_14 S(01) StringType",
"12 CRI1-MSONT_TT_15 7(18) IntegerType ",
"13 QSDRI1-SEDNS_15 S(01) StringType",
"14 WSQSRI1-DEVSISE S(05) StringType")
The function related to data type calculation is:
def dataType (input:String) : String = (input.charAt(0),
input.contains('F')) match {
case ('S', _) => "StringType"
case ('7', false) => "IntegerType"
case ('7', true) => "FloatType"
case _ => "UnknowType"
}
推荐答案
所有您需要的是将确切的字符串值传递给dataType
函数,如下所示
All you need is pass the exact string value to the dataType
function as below
input.map(line => line +" "+ dataType(line.split(" ")(2)))
应该给您
17 SD1-MONT_FF_13 7(14) IntegerType
17 QXRI1-SEDDS_13 S(01) StringType
17 XFDRI1-MONDT_TT_14 7(18) IntegerType
17 SQXI1-SSENS_14 S(01) StringType
12 CRI1-MSONT_TT_15 7(18) IntegerType
13 QSDRI1-SEDNS_15 S(01) StringType
14 WSQSRI1-DEVSISE S(05) StringType
这篇关于在scala中对行进行递归计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文