避免在scala中使用可变变量

rak*_*shr 2 for-loop scala

我有一段这样的代码

def filter(t: String) : Boolean = {
    var found = false;
    for(s <- listofStrings) {
      if ( t.contains(s)) { found = true}
    }
    found
  }
Run Code Online (Sandbox Code Playgroud)

编译器发出警告,说明使用可变变量并不是一种好的做法.我该如何避免这种情况?

免责声明:我在作业中使用了此代码的变体,并完成了提交.我想知道正确的做法是什么

Edu*_*rdo 8

你可以这样做:

def filter(t:String) = listofStrings.exists(t.contains(_))
Run Code Online (Sandbox Code Playgroud)