この質問は重複している可能性がありますが、私は答えを見つけられません。 Scalaの私の無知の知識を許してください。私はかなり初心者です。作成方法オプションリストのリスト[Double] forループScala
私の目的は、長さの異なる2つのリストをループし、List[List[Option[Double]]]
を返すことです。
これまでの私のコード:
def getDoubles(symbol: String, year: Int): Option[Double] = {
return Some(140.0)
}
// this method loops over list of strings and range of time. returns list of list of Option[Double]
def checkList(CSV: List[String], time: Range): List[List[Option[Double]]] = {
// this variable is whats going to be returned at the end
var listOfLists = List[List[Option[Double]]]()
// So we are going to loop over our list and our time, and link each element in our list to all the elements in our time range
for {
i < -CSV
j < -time
// the method getDoubles is pretty long, so i just made another version simplfying my question.
// get doubles bascially returns Option of doubles
}
yield (listOfLists = getDoubles(j, i)::listOfLists)
return listOfLists
}
上記のコード、私はより洗練されたデータとそれを呼び出すとき、それが返されます。
Vector(
Some(313.062468),
Some(27.847252),
Some(301.873641),
Some(42.884065),
Some(332.373186),
Some(53.509768)
)
をしかし、私はこのような何かを返したい:
List(
List(Some(313.062468), Some(27.847252)),
List(Some(301.873641), Some(42.884065)),
List(Some(332.373186), Some(53.509768))
)
どうすればいいですか?
このケースは簡単ですが、将来的には、あなたの「洗練されたデータ」を提供してください(例として、人々は答えのコードで簡単に試してみることができるようにします)。 – laughedelic