Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.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
Scala 递归构建MongoDBObject_Scala_Recursion_Casbah - Fatal编程技术网

Scala 递归构建MongoDBObject

Scala 递归构建MongoDBObject,scala,recursion,casbah,Scala,Recursion,Casbah,如何递归地附加到Casbah中的DBObject,然后返回一个附加了每个列表元素的MongoDBObject 请注意,下面的代码无法编译或工作,但它旨在显示我所需的代码 def foo( pairs: List[(String, Int)]): List[MongoDBObject] = { def go( ps: List[(String, Int)], acc: List[MongoDBObject]): List[MongoDBObject] = ps match {

如何递归地附加到Casbah中的
DBObject
,然后返回一个附加了每个列表元素的
MongoDBObject

请注意,下面的代码无法编译或工作,但它旨在显示我所需的代码

def foo( pairs: List[(String, Int)]): List[MongoDBObject] = {
  def go( ps: List[(String, Int)], acc: List[MongoDBObject]): List[MongoDBObject] =
      ps match {
         case x :: xs if(x._1 == "BAD") => go(xs, acc)
         case x :: xs =>  go(xs, MongoDBObject(x._1 -> x._2) :+ acc) /* error line */
         case Nil => acc
  }
}

val pairsList: List[MongoDBObject] = foo( getPairs() ) // assume getPairs() is defined
val builder = // do something to convert pairsList -> MongoDBObject (maybe a fold?)
val results = collection.find(builder) 
当我尝试上述方法时,我在第二个case语句中看到了以下编译时错误

[myApp] $ compile
[info] Compiling 1 Scala source to ...
[error] c:\development\myApp\Test.scala:85: overloaded method value apply with alternatives:
[error]   [A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply) <
: String, B(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)](e
lems: List[(A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply),
 B(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply))])com.mongo
db.casbah.commons.Imports.DBObject <and>
[error]   [A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply) <
: String, B(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)](e
lems: (A(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply), B(in
 method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply)(in method apply))*)com.mongodb.ca
sbah.commons.Imports.DBObject
[error]  cannot be applied to (com.mongodb.casbah.query.Imports.DBObject with com.mongodb.casbah.query.dsl.QueryExpressionObject)
[error]   go(xs, acc :+ MongoDBObject(elemMatch))
[error]                                                                               ^
[error] one error found
[myApp]$compile
[信息]正在将1个Scala源代码编译为。。。
[错误]c:\development\myApp\Test.scala:85:重载方法值应用于替代项:
[错误][A(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)<
:字符串,B(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中);(e)
lems:列表[(A)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用),
(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用))com.mongo
db.casbah.commons.Imports.DBObject
[错误][A(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)<
:字符串,B(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中);(e)
lems:(A)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中)(在方法应用中),B(在
方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用)(方法适用))com.mongodb.ca
sbah.commons.Imports.DBObject
[错误]无法应用于(com.mongodb.casbah.query.Imports.DBObject和com.mongodb.casbah.query.dsl.QueryExpressionObject)
[错误]转到(xs,acc:+MongoDBObject(elemMatch))
[错误]^
[错误]发现一个错误

如果要将新对象附加到列表中,应执行以下操作:

MongoDBObject(x._1 -> x._2) :: acc
在我的例子中,我尝试了
go(xs,MongoDBObject(elemMatch)::acc)
,但是我得到了上面相同的错误。
elemMatch
是有效的,因为您以前曾帮助我创建过它。
MongoDBObject(“a”->“b”):List(MongoDBObject(“c”->“d”)
适合我。也许需要一些澄清。