Scalaz module

Kantan.regex 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.regex-scalaz" % "0.5.3"

You then need to import the corresponding package:

import kantan.regex.scalaz._

\/ decoder

The scalaz module provides a GroupDecoder instance for \/: for any type A and B that each have a GroupDecoder instance, there exists a GroupDecoder instance for A \/ B.

First, a few imports:

import scalaz._
import kantan.regex.implicits._

We can then simply write the following:

"[123] [true]".evalRegex[Int \/ Boolean](rx"\[(\d+|true|false)\]", 1).foreach(println _)
// Right(-\/(123))
// Right(\/-(true))

This also applies to MatchDecoder instances:

"(1, true) and then (2, foo)".evalRegex[(Int, Boolean) \/ (Int, String)](rx"\((\d+), ([a-z]+)\)").foreach(println _)
// Right(-\/((1,true)))
// Right(\/-((2,foo)))

Maybe decoder

The scalaz module provides a GroupDecoder instance for Maybe: for any type A that has a GroupDecoder instance, there exists a GroupDecoder instance for Maybe[A].

"[123], []".evalRegex[Maybe[Int]](rx"\[(\d+)?\]", 1).foreach(println _)
// Right(Just(123))
// Right(Empty())

The same is true for MatchDecoder, although I can’t really think of an example for this odd concept.

Scalaz instances

The following instance for cats type classes are provided:


Other tutorials: