简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM