ApplicativePlus

ApplicativePlus is a concept from functional programming that combines the capabilities of applicative functors — Applicative](applicative.md) and monadic combinators — PlusEmpty](plus-empty.md). It adds the ability to combine values within a context and also to work with an "empty" value (an identity element).

Main characteristics of ApplicativePlus:

Examples of ApplicativePlus

Formal Definition

ApplicativePlus is a monoidal applicative functor, Applicative combined with PlusEmpty.

ApplicativePlus must satisfy the laws of its parents.

Code

trait ApplicativePlus[F[_]] extends Applicative[F], PlusEmpty[F]

Example with List

given ApplicativePlus[List] with
  def unit[A](a: => A): List[A] = List(a)

  def apply[A, B](fab: List[A => B])(fa: List[A]): List[B] =
    fab.flatMap { aToB => fa.map(aToB) }

  def plus[A](fa1: List[A], fa2: => List[A]): List[A] = fa1 ++ fa2

  def empty[A]: List[A] = List.empty[A]

Schema

classDiagram
    class InvariantFunctor~A~{
        +imap[B](fa: F[A], f: A => B, g: B => A) F[B]
    }
    class Semigroupal~A~{
        +product[A, B](fa: F[A], fb: F[B]) F[Tuple2[A, B]]
    }
    class Functor~A~{
        +map[B](fa: F[A], f: A => B) F[B]
    }
    InvariantFunctor <|-- Functor    
    class InvariantApplicative~A~{
        +xunit0[A](a: => A) F[A]
    }
    InvariantFunctor <|-- InvariantApplicative   
    class Apply~A~{
        +apply[A, B](fab: F[A => B])(fa: F[A]) F[B]
    }
    Functor <|-- Apply
    Semigroupal <|-- Apply
    class Applicative~A~{
        +unit[A](a: => A) F[A]
    }
    Apply <|-- Applicative
    InvariantApplicative <|-- Applicative
    class PlusEmpty~A~{
        +empty[A]: F[A]
    }
    class ApplicativePlus~A~{
        +plus[A](fa1: F[A], fa2: => F[A]) F[A]
    }
    Applicative <|-- ApplicativePlus
    PlusEmpty <|-- ApplicativePlus