PlusEmpty

PlusEmpty is an extension of the Plus concept in functional programming that adds the ability to work with an "empty" value (an identity element) within a context. This value serves as a unit for the combining operation (plus, combine, or <+>) and is often called an "empty" or "zero" element.

Main characteristics of PlusEmpty:

Examples of PlusEmpty

Formal Definition

A universally quantified monoid.

PlusEmpty must satisfy the laws of a monoid:

Code

trait PlusEmpty[F[_]] extends Plus[F]:
  self =>

  def empty[A]: F[A]

  def monoid[A]: Monoid[F[A]] =
    new Monoid[F[A]]:
      override def combine(f1: F[A], f2: F[A]): F[A] = plus(f1, f2)
      override def empty: F[A] = self.empty[A]

Laws

Example with List

given PlusEmpty[List] with
  def plus[A](fa1: List[A], fa2: => List[A]): List[A] = fa1 ++ fa2
  def empty[A]: List[A] = List.empty[A]

Schema

classDiagram
    class Plus~A~{
        +plus[A](fa1: F[A], fa2: => F[A]) F[A]
    }
    class PlusEmpty~A~{
        +empty[A]: F[A]
    }
    Plus <|-- PlusEmpty

Library Implementations

Implementation in ScalaZ