I have a query result of List[ Tuple3[User, Order, OrderItem] ]
To create a case class instance of an Invoice, its companion object takes a User, Order and a List[OrderItem].
Currently I’m hacking it out something like:
def getInvoice(orderNum: String): Option[Invoice] = {
val res =
dao.byOrderNum(orderNum) // List[ Tuple3[User, OrderEntry, OrderItem] ]
if(!res.isEmpty) {
val(user, order) = (res(0)._1, res(0)._2)
val items = res map { case(_, _, oi: OrderItem) => oi }
Some( Invoices.apply(user, order, items) ) // gets an Invoice
}
else None
}
I could make the query result a List[ Option[Tuple3[User, Order, OrderItem]] ], which would let me flatMap over the result, but not sure what that buys me.
At any rate, must be a more concise/elegant solution to the problem
Thanks
The following should be exactly equivalent:
The key is
headOption, which handles the checking for emptiness in a more idiomatic way (it givesNonefor an empty sequence andSome(xs.head)for a non-empty one).