0

Casbah で a に再帰的に追加し、各リスト要素が追加され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) 

上記のようなことを試したところ、2 番目の 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
4

1 に答える 1

2

新しいオブジェクトをリストに追加する場合は、次のようにします。

MongoDBObject(x._1 -> x._2) :: acc
于 2013-10-15T14:04:44.977 に答える