Toy implementations of the algebraic structures defined in the Fantasy Land specification, mostly borrowed from Haskell libraries.
The following objects each implement a constructor allowing you to lift values into these base objects. Many of the other instances in this library are built on top of these base objects. As such, they implement a similar constructor pattern.
|A value (Identity)|
|An optional value|
|An optional with a default value|
|One of two types of values|
new Default(a) actually returns a constructor for an
Optional that defaults to
a. In the following sections, such constructors-for-constructors will be listed under the heading “Constructors” and all other simple constructors will be listed under “Instances”.
|Given associative binary function |
|Given Semigroup |
|Takes the first value labelled |
|Takes the first |
|Takes the last |
This module re-exports all Monoid instances.
|Given value |
|Given Monoid |
|Lift Semigroup |
|identity function||function composition|
|If the value exists, apply |
|If the value is labelled |
This module re-exports all Applicative instances.
This module re-exports all Monad instances.
|A missing value short-circuits the chain.|
freezes its resulting object.
'use strict', so attempting to mutate any of the structures throws a type error.
./lib/common.js. These throw errors, for example, when arguments are of the wrong type or wrong number are provided.
Note: the test suite itself could be better tested!
The test suite attempts to verify that the instances of algebraic structures defined in this library satisfy the laws defined in the Fantasy Land specification. Additionally, it includes a number of noninstance "sanity checks".
Before running, make sure you have installed the package with
npm install, as the test suite relies on Mocha.
$ make about-testingTestingmake test # Run all instance testsmake test-instances # Run all instance testsmake test-noninstances # Run all noninstance testsmake test-all # Run all testsmake test-all-verbose # Run all tests in verbose modemake testing # Run all tests continuosly
Id:✓ Identity (Functor)✓ Composition (Functor)✓ Identity (Applicative)✓ Composition (Applicative)✓ Homomorphism (Applicative)✓ Interchange (Applicative)✓ Associativity (Chain)✓ Left Identity (Monad)✓ Right Identity (Monad)
Difference:✓ is not a SemigroupRock, Paper, Scissors:✓ is not a Semigroup