Options
All
  • Public
  • Public/Protected
  • All
Menu

The Functor is a type class providing the map operation that allows lifting an f function into the functor context and applying it.

The name is short for "covariant functor".

Example:

const F = functorOf(Option)

F.map(Some(1), x => x + 1) // Some(2)

MUST obey the laws defined in FunctorLaws.

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, Functor, registerTypeClassInstance } from "funfix"

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 these can and should be undefined:
  readonly _funKindF: Box<any>
  readonly _funKindA: T
}

// Type alias defined for readability
type BoxK<T> = HK<Box<any>, T>

// Actual implementation
class BoxFunctor implements Functor<Box<any>> {
  map<A, B>(fa: BoxK<A>, f: (a: A) => B): Box<B> {
    const a = (fa as Box<A>).value
    return new Box(f(a))
  }
}

// Registering global Functor instance for Box
registerTypeClassInstance(Functor)(Box, new BoxFunctor())

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

  • Functor

Implemented by

Index

Methods

Methods

map

  • map<A, B>(fa: HK<F, A>, f: function): HK<F, B>
  • Given a mapping function, transforms the source.

    The map operation must follow these laws:

    • fa.map(id) <-> fa
    • fa.map(f).map(g) <-> fa.map(x => g(f(x)))

    Type parameters

    • A

    • B

    Parameters

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

          • a: A

          Returns B

    Returns HK<F, B>

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