An external system returns Seq[String] (kind of DB, output like CSV/json), it's wrap of base types: string/numbers. I'd rather work with my own model.
object Converter {
type Output = (Int, String, Double) // for instance
def convert(values: List[String]): Output
}
Obviously, I do not want to implement convert-method every time.
Seems like I need something simpler than http://nrinaudo.github.io/tabulate/tut/parsing.html
Is it possible to use HList here? Like conversion sized HList (String:: String:: String:: HNil) into model by defining explicit only Output type.
First of all, the output of
convert
method would have to beOption[Output]
, or some monad ofOutput
(Try
,Either
,scalaz.\/
,scalaz.Validation
, etc.) in case the contents of theSeq[String]
can't be converted toOutput
(wrong length ofSeq
, errors in parsingInt
s orDoubles
, etc.)A possible implementation with
shapeless
would have a typeclass to convertString
to its parameter type, and an auxiliary typeclass to convert anHList
ofString
to theHList
representation ofOutput
with the first typeclass.Here is a sample implementation:
It's simple to upgrade this implementation to return your error monad of choice (or to throw exceptions) instead of returning
Option
And here is how you can use it:
It also works with case classes instead of tuples: