I have a case class like this:
case class Metric(name: String, value: Double, timeStamp: Int) I receive individual components to build metrics in separate lists and zip them to create a list of Metric objects.
def buildMetric(names: Seq[String], values: Seq[Double], ts: Seq[Int]): Seq[Metric] = { (names, values, ts).zipped.toList map { case (name, value, time) => Metric(name, value, time) } } Now I need to add an optional parameter to both buildMetric function and Metric class.
case class Metric(name: String, value: Double, timeStamp: Int, type: Option[Type]) &
def buildMetric(names: Seq[String], values: Seq[Double], ts: Seq[Int], types: Option[Seq[Type]]): Seq[Metric] The idea is that we some times receive a sequence of the type which if present matches the length of names and values lists. I am not sure how to modify the body of buildMetric function to create the Metric objects with type information idiomatically. I can think of a couple of approaches.
Do an if-else on
types.isDefinedand then zip the types withtypes.getwith another list in one condition and leave as above in the other. This makes me write the same code twice.The other option is to simply use a while loop and create a Metric object with
types.map(_(i))passed a last parameter.
So far I am using the second option, but I wonder if there is a more functional way of handling this problem.