斯卡拉如何模仿匿名函数中的三元运算符

时间:2018-08-31 11:13:14

标签: scala

我想通过尝试模仿三元运算符在下面的代码中获得“ a”,但低于错误

scala> val ab="apple"
ab: String = apple

scala> ab.toCharArray.map( x => "aeiou".indexOf(x) >= 0  )
res99: Array[Boolean] = Array(true, false, false, false, true)

scala> ab.toCharArray.map( x => "aeiou".indexOf(x) >= 0 ? x : ' ' )
<console>:1: error: identifier expected but character literal found.
ab.toCharArray.map( x => "aeiou".indexOf(x) >= 0 ? x : ' ' )
                                                       ^

scala>

1 个答案:

答案 0 :(得分:2)

有效的Scala语法为

array1(0,0) = "aaa "
array1(0,1) = "bbb "
array1(0,2) = "ccc "

array1(1,0) = "ddd "
array1(1,1) = "eee "

array1(2,0) = "fff "
array1(2,1) = "ggg "

相反

aaa ddd fff
aaa ddd ggg
aaa eee fff
aaa eee ggg

bbb ddd fff
bbb ddd ggg
bbb eee fff
bbb eee ggg

ccc ddd fff
ccc ddd ggg
ccc eee fff
ccc eee ggg

是Java语法。