(aka "Algebraic JavaScript Specification")
This project specifies interoperability of common algebraic structures:
- Functor
- Monad
An algebra is a set of values, a set of operators that it is closed under and some laws it must obey.
- "value" is any JavaScript value, including any which have the structures defined below.
u.map(function(a) { return a; }))equalsuu.map(function(x) { return f(g(x)); })equalsu.map(g).map(f)
A value which has a functor must provide a map method. The map
method takes one argument:
u.map(f)
-
fmust be a function,- If
fis not a function, the behaviour ofmapis unspecified. fcan return any value.
- If
-
mapmust return a value of the same functor
A value which satisfies the specification of a monad do not need to implement:
- Functor's
map; derivable asfunction(f) { var m = this; return m.then(function(a) { return m.constructor.of(f(a)); })}
of(a).then(f)equalsf(a)m.then(of)equalsmm.then(f).then(g)equalsm.then(function(x) { return f(x).then(g); })
A value which has a monad must provide a then method. The then
method takes one argument:
m.then(f)
-
fmust be a function which returns a value- If
fis not a function, the behaviour ofthenis unspecified. fmust return a value of the same monad
- If
-
thenmust return a value of the same monad
A value which has a monad must provide a constructor object. The
constructor object must have an of method which takes one
argument:
m.constructor.of(a)
-
ofmust provide a value of the same monad- No parts of
ashould be checked
- No parts of
- If there's more than a single way to implement the methods and laws, the implementation should choose one and provide wrappers for other uses.
- It's discouraged to overload the specified methods. It can easily result in broken and buggy behaviour.
- It is recommended to throw an exception on unspecified behaviour.
- An
Idcontainer which implements all methods is provided inid.js.