Scalaz Bind[Seq] typeclass

The collections library does backflips to accommodate subtyping: when you use map on a specific collection type (list, map, etc.), you’ll (usually) get the same type back. It manages this through the use of an extremely complex inheritance hierarchy together with type classes like CanBuildFrom. It gets the job done (at least arguably), but the … Read more

Why isn’t Validation a Monad?

As discussed in the Scalaz group, the problem seems to be that ap would accumulate errors whereas (pseudo-)monadic composition would only operate on the value part of Validation. Therefore, one cannot be expressed in terms of the other and thus no monad instance exists for Validation.

Scalaz state monad examples

I assume, scalaz 7.0.x and the following imports (look at answer history for scalaz 6.x): import scalaz._ import Scalaz._ The state type is defined as State[S, A] where S is type of the state and A is the type of the value being decorated. The basic syntax to create a state value makes use of … Read more

Using context bounds “negatively” to ensure type class instance is absent from scope

I eventually solved this using an ambiguity-based solution that doesn’t require prioritizing using inheritance. Here is my attempt at generalizing this. We use the type Not[A] to construct negative type classes: import scala.language.higherKinds trait Not[A] trait Monoid[_] // or import scalaz._, Scalaz._ type NotMonoid[A] = Not[Monoid[A]] trait Functor[_[_]] // or import scalaz._, Scalaz._ type NotFunctor[M[_]] … Read more

Update operations on a Scala Case Class

Fairly straightforward Scalaz solution (not very general) You can use a semigroup instance to wrap up a lot of the details: import scalaz._, Scalaz._ case class Foo(a: Option[String], b: Option[String], c: Option[String]) implicit object fooSemigroup extends Semigroup[Foo] { def fromFoo(f: Foo) = (f.a.fst, f.b.fst, f.c.fst) def toFoo(t: (FirstOption[String], FirstOption[String], FirstOption[String])) = Foo(t._1.value, t._2.value, t._3.value) def … Read more