Scalaz module

Kantan.csv has a scalaz module that is, in its current incarnation, fairly bare bones: it provides decoders for Maybe and \/ as well as a few useful type class instances.

The scalaz module can be used by adding the following dependency to your build.sbt:

libraryDependencies += "com.nrinaudo" %% "kantan.csv-scalaz" % "0.7.0"

You then need to import the corresponding package:

import kantan.csv.scalaz._

\/ codecs

For any two types A and B that each have a CellEncoder, there exists a CellEncoder[A \/ B]. If A and B each have a RowEncoder, there exists a RowEncoder[A \/ B].

By the same token, for any two types A and B that each have a CellDecoder, there exists a CellDecoder[A \/ B]. If A and B each have a RowDecoder, there exists a RowDecoder[A \/ B].

First, a few imports:

import scalaz._
import kantan.csv._
import kantan.csv.ops._

We can then simply write the following:

"1,2\n4,true".readCsv[List, (Int, Int \/ Boolean)](rfc)
// res0: List[ReadResult[(Int, Int \/ Boolean)]] = List(
//   Right(value = (1, -\/(a = 2))),
//   Right(value = (4, \/-(b = true)))
// )

"1,2\n4,true".readCsv[List, (Int, Int) \/ (Int, Boolean)](rfc)
// res1: List[ReadResult[(Int, Int) \/ (Int, Boolean)]] = List(
//   Right(value = -\/(a = (1, 2))),
//   Right(value = \/-(b = (4, true)))
// )

Maybe decoder

For any type A that has:

You can write, for example:

"1,2\n3,".readCsv[List, (Int, Maybe[Int])](rfc)
// res2: List[ReadResult[(Int, Maybe[Int])]] = List(
//   Right(value = (1, Just(get = 2))),
//   Right(value = (3, Empty()))
// )

Scalaz instances

The following instance for cats type classes are provided:


Other tutorials: