Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不使用for(循环;if条件)的情况下为scala中的迭代编写此代码,并且仍然正确地使用yield_Scala - Fatal编程技术网

如何在不使用for(循环;if条件)的情况下为scala中的迭代编写此代码,并且仍然正确地使用yield

如何在不使用for(循环;if条件)的情况下为scala中的迭代编写此代码,并且仍然正确地使用yield,scala,Scala,嗨,我是一个Scala的学生,在这方面没有太多的经验 我一直想做这样的东西 object FileMatcher { private def filesHere = (new java.io.File(".")).listFiles private def filesMatching(matcher: String => Boolean) = for (file <- filesHere; if matcher(file.getName))

嗨,我是一个Scala的学生,在这方面没有太多的经验

我一直想做这样的东西

object FileMatcher {
    private def filesHere = (new java.io.File(".")).listFiles

    private def filesMatching(matcher: String => Boolean) =
        for (file <- filesHere; if matcher(file.getName))
            yield file.getName

    def filesEnding(query: String) = filesMatching(_.endsWith(query))
    def filesContaining(query: String) = filesMatching(_.contains(query))
    def filesRegex(query: String) = filesMatching(_.matches(query))
}
按照代码显示的顺序是

***.scala
接下来是

(),(),(),(),***.scala,(),(),()
因为if正在返回它的值。

这个怎么样:

private def filesMatching(matcher: String => Boolean) =
  filesHere.filter(file => matcher(file.getName)).map(file => file.getName)
第一个表达式

for (file <- filesHere; if matcher(file.getName))
        yield file.getName
其中
file=>file.getName
返回筛选集合上的
String

第二个呢

for (file <- filesHere)
             yield
        if (matcher(file.getName))
            file.getName

其中
file=>if(matcher(file.getName))file.getName
重新运行
输出中显示为
()

的任何
。。。好的,很好。但是,如何将For与嵌套If一起使用,以仅生成If传递的值?有可能吗?不使用这种For(loop;if)语法是否可以做到这一点。for循环有两种形式:“for(枚举)exp”和“for(枚举)yield exp”。除了第二种形式,收益率不能出现在任何地方。它不是自己的语句,而是for循环构造的“固定”部分。它是“enumeration.map(exp)”的语法糖。只有使用
for(expr1;if expr2)yeild expr2
语法才能实现,该语法被转换为
expr1.filter(expr2.map(expr3)
。请阅读
的详细解释,以获得理解
。谢谢!但我的问题仍然存在。这是唯一的办法吗?我不能使用For和if吗?中间收益率?
for (file <- filesHere; if matcher(file.getName))
        yield file.getName
filesHere.filter(file => matcher(file.getName)).map(file => file.getName)
for (file <- filesHere)
             yield
        if (matcher(file.getName))
            file.getName
filesHere.map(file => if (matcher(file.getName)) file.getName)