Options
All
  • Public
  • Public/Protected
  • All
Menu

Applicative functor type class.

Allows application of a function in an Applicative context to a value in an Applicative context.

References:

Example:

const F = applicativeOf(Option)

F.ap(F.pure(1), F.pure((x: number) => x + 1)) // Some(2)

Note that having an Applicative instance implies Functor and Apply implementations are also available, which is why Applicative is a subtype of Functor and Apply.

Implementation notes

Even though in TypeScript the Funfix library is using abstract class to express type classes, when implementing this type class it is recommended that you implement it as a mixin using "implements", instead of extending it directly with "extends". See TypeScript: Mixins for details and note that we already have applyMixins defined.

Implementation example:

import {
  HK, Applicative,
  registerTypeClassInstance,
  applyMixins
} from "funfix"

// Type alias defined for readability.
// HK is our encoding for higher-kinded types.
type BoxK<T> = HK<Box<any>, T>

class Box<T> implements HK<Box<any>, T> {
  constructor(public value: T) {}

  // Implements HK<Box<any>, A>, not really needed, but useful in order
  // to avoid type casts. Note they can and should be undefined:
  readonly _funKindF: Box<any>
  readonly _funKindA: T
}

class BoxApplicative implements Applicative<Box<any>> {
  pure<A>(a: A): Box<A> { return new Box(a) }

  ap<A, B>(fa: BoxK<A>, ff: BoxK<(a: A) => B>): Box<B> {
    const a = (fa as Box<A>).value
    const f = (ff as Box<(a: A) => B>).value
    return new Box(f(a))
  }

  // Mixed-in, as these have default implementations
  map: <A, B>(fa: BoxK<A>, f: (a: A) => B) => Box<B>
  map2: <A, B, Z>(fa: BoxK<A>, fb: BoxK<B>, f: (a: A, b: B) => Z) => Box<Z>
  product: <A, B> (fa: BoxK<A>, fb: BoxK<B>) => Box<[A, B]>
  unit: () => Box<void>
}

// Call needed in order to implement `map`, `map2`, `product` and `unit`,
// using the default implementations defined by `Applicative`, because
// we are using `implements` instead of `extends` above and
// because in this sample we want the default implementations,
// but note that you can always provide your own
applyMixins(BoxApplicative, [Applicative])

// Registering global Applicative instance for Box, needed in order
// for the `functorOf(Box)`, `applyOf(Box)` and `applicativeOf(Box)`
// calls to work
registerTypeClassInstance(Applicative)(Box, new BoxApplicative())

We are using implements in order to support multiple inheritance and to avoid inheriting any static members. In the Flow definitions (e.g. .js.flow files) for Funfix these type classes are defined with "interface", as they are meant to be interfaces that sometimes have default implementations and not classes.

Credits

This type class is inspired by the equivalent in Haskell's standard library and the implementation is inspired by the Typelevel Cats project.

Type parameters

  • F

Hierarchy

  • Applicative

Implements

Implemented by

Index

Properties

Methods

Properties

map2

map2: function

Mixed-in from Apply.map2.

Type declaration

    • <A, B, Z>(fa: HK<F, A>, fb: HK<F, B>, f: function): HK<F, Z>
    • Type parameters

      • A

      • B

      • Z

      Parameters

      • fa: HK<F, A>
      • fb: HK<F, B>
      • f: function
          • (a: A, b: B): Z
          • Parameters

            • a: A
            • b: B

            Returns Z

      Returns HK<F, Z>

product

product: function

Mixed-in from Apply.product.

Type declaration

    • <A, B>(fa: HK<F, A>, fb: HK<F, B>): HK<F, [A, B]>
    • Type parameters

      • A

      • B

      Parameters

      • fa: HK<F, A>
      • fb: HK<F, B>

      Returns HK<F, [A, B]>

Methods

ap

  • ap<A, B>(fa: HK<F, A>, ff: HK<F, function>): HK<F, B>

map

  • map<A, B>(fa: HK<F, A>, f: function): HK<F, B>
  • Inherited from Functor.map.

    Type parameters

    • A

    • B

    Parameters

    • fa: HK<F, A>
    • f: function
        • (a: A): B
        • Parameters

          • a: A

          Returns B

    Returns HK<F, B>

pure

  • pure<A>(a: A): HK<F, A>
  • Lifts a strict value A into the F<A> context.

    Type parameters

    • A

    Parameters

    • a: A

    Returns HK<F, A>

unit

  • unit(): HK<F, void>
  • Shorthand for pure<void>(undefined), provided for convenience and because implementations can override the default for optimization purposes.

    Returns HK<F, void>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc