Search code examples
scalafunctional-programmingscala-cats

Applicative with zero


I was just working on application and encountered case where I'd like to abstract over appendable collections. I came up with the following type class.

trait AppendableCollection[F[_]] {
  def empty[A]: F[A]
  def append[A](fa: F[A])(a: A): F[A]
}

object AppendableCollection {
  implicit val reversedListCollection = new Collection[List] {
    def empty[A] = Nil
    def append[A](fa: List[A])(a: A) = a :: fa
  }
}

It kinda looks like Applicative with zero but and I bet there is something like this available in cats or its ecosystem?


Solution

  • Provided there is pure(a: A): F[A], looks similar to MonoidK

    trait MonoidK[F[_]] {
      def empty[A]: F[A]
      def combineK[A](x: F[A], y: F[A]): F[A]
    }
    

    https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/MonoidK.scala#L25