无论评估哪种情况,都要修改匹配结果

时间:2014-09-05 14:06:50

标签: scala

简单一个!鉴于以下内容:

print(1 to 100 map {
    case n if n % 10 == 0 => "Divisible by ten!"
    case n if n % 5 == 0 => "Divisible by five!"
    case n => n
})

制作它的最良心方式是什么,以便每次迭代后面都有换行符?我希望尽量减少必要的表达式块数。

1 个答案:

答案 0 :(得分:7)

这是一种方式:

1 to 100 map {
    case n if n % 10 == 0 => "Divisible by ten!"
    case n if n % 5 == 0 => "Divisible by five!"
    case n => n 
} foreach println _

或者

println(
    1 to 100 map {
        case n if n % 10 == 0 => "Divisible by ten!"
        case n if n % 5 == 0 => "Divisible by five!"
        case n => n
    } mkString "\n"
)