Methods
(static) chain(m, f) → {Object}
Sequentially compose two actions, discarding any value produced by the first, like sequencing
operators (such as the semicolon) in imperative languages.
Haskell> (>>) :: m a -> m b -> m b
Parameters:
Name | Type | Description |
---|---|---|
m |
Object | A monad |
f |
function | A function to call that ignores the injected value of the monad |
Returns:
A new monad of the same type, the result of calling the function
- Type
- Object
(static) Do(m) → {DoBlock}
Wrap a monad in a special container for the purpose of chaining actions, in imitation of the syntactic sugar provided by Haskell's "do" notation.
Parameters:
Name | Type | Description |
---|---|---|
m |
Object | A monad |
Returns:
A monadic context in which to chain actions
- Type
- DoBlock
Example
const j = just(10);
const doubleJust = x => just(x * 2);
const minusOne = x => just(x - 1);
const lst = list(1,2,3);
const plusOne = x => list(x + 1);
const doubleList = x => list(x * 2);
const put = x => {
print(x);
return just(x);
}
const b1 = Do(j).flatMap(doubleJust).flatMap(minusOne);
const b2 = Do(j).flatMap(doubleJust).chain(j).flatMap(minusOne);
const b3 = Do(lst).flatMap(plusOne).flatMap(doubleList);
const b4 = Do(lst).flatMap(plusOne).chain(lst).flatMap(doubleList);
const b5 = Do(j).inject(100);
const b6 = Do(lst).inject(100);
print(b1); // => Maybe number >>= Just 19
print(b2); // => Maybe number >>= Just 9
print(b3); // => [number] >>= [4:6:8:[]]
print(b4); // => [number] >>= [2:4:6:2:4:6:2:4:6:[]]
print(b5); // => Maybe number >>= Just 100
print(b6); // => [number] >>= [100:[]]
Do(j)
.flatMap(put) // => 10
.flatMap(doubleJust)
.flatMap(put) // => 20
.chain(j)
.flatMap(put) // => 10
.flatMap(minusOne)
.flatMap(put) // => 9
.flatMap(doubleJust)
.flatMap(put); // => 18
(static) flatMap(m, f) → {Object}
Sequentially compose two actions, passing the value produced by the first action as an argument
to the second action. This function is variously referred to as bind, map, and flatMap. This
library uses flatMap
in order to avoid conflicts with JavaScript's built-in bind
and map
methods.
Haskell> (>>=) :: m a -> (a -> m b) -> m b
Parameters:
Name | Type | Description |
---|---|---|
m |
Object | A monad |
f |
function | A function to map over the injected value of the monad. This function must also return a monad |
Returns:
A new monad of the same type, the result of mapping the function over the original, injected value
- Type
- Object
(static) flatMapFlip(f, m) → {Object}
The same as flatMap
but with the arguments interchanged.
Haskell> (=<<) :: Monad m => (a -> m b) -> m a -> m b
Parameters:
Name | Type | Description |
---|---|---|
f |
function | A function to map over the injected value of the monad |
m |
Object | A monad |
Returns:
A new monad of the same type, the result of mapping the function over the original, injected value
- Type
- Object
(static) inject(m, a) → {Object}
Inject a value into a monadic context.
Haskell> return :: a -> m a
Parameters:
Name | Type | Description |
---|---|---|
m |
Object | A monad |
a |
* | The value to inject |
Returns:
A new monad of the same type with the value injected
- Type
- Object
(static) join(m) → {Object}
Remove one level of monadic structure from a monad, projecting its bound argument into the outer
level.
Haskell> join :: Monad m => m (m a) -> m a
Parameters:
Name | Type | Description |
---|---|---|
m |
Object | A monad (wrapping another monad) |
Returns:
The wrapped monad on its own
- Type
- Object
Example
const m = just(10); // => Just 10
const n = just(m); // => Just Just 10
join(n); // => Just 10
join(m); // => *** Error: 'Just 10' is not a valid argument to function 'join'.
(static) liftM(f, m) → {Object}
Convert, or lift, a function into a monad.
Haskell> liftM :: Monad m => (a1 -> r) -> m a1 -> m r
Parameters:
Name | Type | Description |
---|---|---|
f |
function | The function to convert into a monad |
m |
Object | The monad to convert the function into |
Returns:
A new monad containing the result of mapping the function over the monad
- Type
- Object
Example
const mb = just(1);
const lst = list(1,2,3);
const doubleJust = x => just(x * 2);
const doubleList = x => list(x * 2);
liftM(doubleJust, mb); // => Just Just 2
liftM(doubleList, lst); // => [[2:[]]:[4:[]]:[6:[]]:[]]
(static) Monad() → {boolean}
A monad is an abstract datatype of sequential actions. Instances of Monad
must define a
flatMap
method as well as all the required methods for Functor
and Applicative
.
Parameters:
Type | Description |
---|---|
* | Any object |
Returns:
true
if an object is an instance of Monad
and false
otherwise
- Type
- boolean