Skip to content

Applicative

funstruct.typeclasses.applicative

Applicative — independent computations combined in context.

pure: A → F[A] ap: F[A → B] → F[A] → F[B]

Applicative

Bases: Functor

pure + ap, with map derived from ap + pure.

Source code in funstruct/typeclasses/applicative.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Applicative(Functor):
    """pure + ap, with map derived from ap + pure."""

    @abstractmethod
    def pure(self, value) -> object: ...

    @abstractmethod
    def ap(self, ff, fa) -> object:
        """F[A → B] → F[A] → F[B]"""
        ...

    def map(self, fa, f: Callable) -> object:
        return self.ap(self.pure(f), fa)

    def map2(self, fa, fb, f: Callable) -> object:
        return self.ap(self.map(fa, lambda a: lambda b: f(a, b)), fb)

    def product(self, fa, fb) -> object:
        return self.map2(fa, fb, lambda a, b: (a, b))

ap(ff, fa) abstractmethod

F[A → B] → F[A] → F[B]

Source code in funstruct/typeclasses/applicative.py
21
22
23
24
@abstractmethod
def ap(self, ff, fa) -> object:
    """F[A → B] → F[A] → F[B]"""
    ...