• Public
  • Public/Protected
  • All

The CoflatMap type class, a weaker version of Comonad, exposing coflatMap, but not extract.

This type class is exposed in addition to Comonad because there are data types for which we can't implement extract, but that could still benefit from an coflatMap definition.

MUST obey the laws defined in CoflatMapLaws.

Note that having a CoflatMap instance implies that a Functor implementation is also available, which is why CoflatMap is a subtype of Functor.

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

class BoxCoflatMap implements CoflatMap<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))

  coflatMap<A, B>(fa: BoxK<A>, ff: (a: BoxK<A>) => B): BoxK<B> {
    return new Box(Success(ff(fa)))

  coflatten<A>(fa: BoxK<A>): BoxK<BoxK<A>> {
    return new Box(Success(fa))

// At the moment of writing, this call is not needed, but it is
// recommended anyway to future-proof the code ;-)
applyMixins(BoxCoflatMap, [CoflatMap])

// Registering global CoflatMap instance for Box, needed in order
// for the `coflatMapOf(Box)` calls to work
registerTypeClassInstance(CoflatMap)(Box, new BoxCoflatMap())

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.


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


  • CoflatMap


Implemented by






map: function

Inherited from Functor.map.

Type declaration

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

      • A

      • B


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

            • a: A

            Returns B

      Returns HK<F, B>



  • coflatMap<A, B>(fa: HK<F, A>, ff: function): HK<F, B>
  • coflatMap is the dual of flatMap on FlatMap.

    It applies a value in a context to a function that takes a value in a context and returns a normal value.

    Type parameters

    • A

    • B


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

          • a: HK<F, A>

          Returns B

    Returns HK<F, B>


  • coflatten<A>(fa: HK<F, A>): HK<F, HK<F, A>>
  • coflatten is the dual of flatten on FlatMap.

    Whereas flatten removes a layer of F, coflatten adds a layer of F.

    Type parameters

    • A


    • fa: HK<F, A>

    Returns HK<F, HK<F, A>>


  • 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