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> { returnnew Box(a) }
ap<A, B>(fa: BoxK<A>, ff: BoxK<(a: A) => B>): Box<B> {
consta = (fa as Box<A>).valueconstf = (ff as Box<(a: A) => B>).valuereturnnewBox(f(a))
}
// Mixed-in, asthesehavedefaultimplementationsmap: <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.
Applicativefunctor type class.Allows application of a function in an Applicative context to a value in an
Applicativecontext.References:
Example:
const F = applicativeOf(Option) F.ap(F.pure(1), F.pure((x: number) => x + 1)) // Some(2)Note that having an
Applicativeinstance implies Functor and Apply implementations are also available, which is whyApplicativeis a subtype ofFunctorandApply.Implementation notes
Even though in TypeScript the Funfix library is using
abstract classto 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 haveapplyMixinsdefined.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
implementsin order to support multiple inheritance and to avoid inheriting anystaticmembers. In the Flow definitions (e.g..js.flowfiles) 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.