Using Scala applicative to convert List[Object] to case class

Viewed 429

I am writing a Scala Play Framework application that uses Datomic as a back-end database.

When I query Datomic, it returns "rows" as java.util.Lists of Lists of java.lang.Objects. For example:

[
  [ "Joe", "1970-04-15" ],
  [ "Mary", "1975-06-01" ]
]

I would like to convert these to case classes in the most Scala-idiomatic way possible.

Currently my code looks like this:

import org.joda.time._
case class Person(name: String, dateOfBirth: LocalDate)

// assuming the query returns 1 row, get the row List
val row = result.iterator.next  // e.g.: [ "Joe", "1970-04-15" ]
User(row.get(0).asInstanceOf[String],
     LocalDate.parse(row.get(1).asInstanceOf[String]))

That's sort of ugly and error-prone.

Is there a way to use applicative functors to achieve the same thing? Are applicative functors the best way to do this? Is there a better way?

2 Answers
Related