用于理解的scala模式匹配
在scala中,您是否可以有一个用于理解的方法来迭代对象列表,然后根据元素属性之一的类型生成值数组?假设我有一个元素列表,每个元素都有一个属性,属性可以是不同的类型用于理解的scala模式匹配,scala,pattern-matching,for-comprehension,Scala,Pattern Matching,For Comprehension,在scala中,您是否可以有一个用于理解的方法来迭代对象列表,然后根据元素属性之一的类型生成值数组?假设我有一个元素列表,每个元素都有一个属性,属性可以是不同的类型 for (element <- elementList) element.attribute match { case a: Type1 => "Type1" case a => "All Types" } 为什么不使用从列表(元素)到列表(字符串)的映射函数 如果您不想从List(String)获取数组,那
for (element <- elementList) element.attribute match {
case a: Type1 => "Type1"
case a => "All Types"
}
为什么不使用从
列表(元素)
到列表(字符串)
的映射函数
如果您不想从
List(String)
获取数组,那么您就拥有了toArray
函数,为什么不使用List(Element)
到List(String)
的映射函数呢
(for (element <- elementList) yield element.attribute match {
case a: Type1 => "Type1"
case a => "All Types"
}).toArray
如果您不想从
列表(字符串)
中获取数组,那么您就有了toArray
函数,只需生成一个结果。。。并可能转换为数组
(for (element <- elementList) yield element.attribute match {
case a: Type1 => "Type1"
case a => "All Types"
}).toArray
(用于(元素“类型1”)
案例a=>“所有类型”
}).托雷
您所要做的就是生成一个结果,
并可能转换为数组
(for (element <- elementList) yield element.attribute match {
case a: Type1 => "Type1"
case a => "All Types"
}).toArray
(用于(元素“类型1”)
案例a=>“所有类型”
}).托雷
问题是什么?我写的东西不起作用。问题是如何制作数组问题是什么?我写的东西不起作用。问题是如何制作数组所以这仅仅是:元素列表映射{{uu.attribute match{/*cases*/}。toArray
,对吗?所以这仅仅是:元素列表映射{{{.attribute match{/*cases*/}}.toArray
,对吗?