在Perl我能做到
my @l = qw( str1 str2 str3 str4 )
Run Code Online (Sandbox Code Playgroud)
在Ruby中
l = %w{ str1 str2 str3 str4 }
Run Code Online (Sandbox Code Playgroud)
但是在Scala看起来我已经被困住了
val l = List( "str1", "str2", "str3", "str4" )
Run Code Online (Sandbox Code Playgroud)
我真的需要所有那些"s和,s吗?
0__*_*0__ 16
你可以做到
implicit class StringList(val sc: StringContext) extends AnyVal {
def qw(): List[String] =
sc.parts.flatMap(_.split(' '))(collection.breakOut)
}
qw"str1 str2 str3"
Run Code Online (Sandbox Code Playgroud)
或者通过隐式类:
implicit class StringList(val s: String) extends AnyVal {
def qw: List[String] = s.split(' ').toList
}
"str1 str2 str3".qw
Run Code Online (Sandbox Code Playgroud)
(两者都需要Scala 2.10,尽管第二个可以适用于Scala 2.9)
| 归档时间: |
|
| 查看次数: |
445 次 |
| 最近记录: |