TsMonad - fun-size monads library for TypeScript
npm install @magda/tsmonadI'm not seeking maintainer to take over - of course feel free to fork if you'd like to continue developing TsMonad.
I won't presume to attempt a monad tutorial here. There are several online - I recommend Douglas Crockford's Monads & Gonads talk.
Node:
var TsMonad = require('tsmonad');
Browser:
TypeScript definitions:
///
``ts
var turns_out_to_be_100 = Maybe.just(10)
.caseOf({
just: n => n * n,
nothing: () => -1
});
var turns_out_to_be_a_piano = Maybe.nothing
.caseOf({
just: n => n * n,
nothing: () => -1 // joke, it's negative one not a piano
});
var turns_out_to_throw_a_compiler_error = Maybe.just(321)
.caseOf({
just: n => 999,
// tsc will tell you that this "does not implement 'nothing'"
// helping to enforce correct handling of all possible paths
});
`
The Maybe monad can simplify processing of values that may not exist:
`ts`
var canRideForFree = user.getAge() // user might not have provided age, this is a Maybe
.bind(age => getBusPass(age)) // not all ages have a bus pass, this is a Maybe
.caseOf({
just: busPass => busPass.isValidForRoute('Weston'),
nothing: () => false
});
Without Maybe, this would be something like:
`ts
var canRideForFree,
age = user.getAge(); // might be null or undefined
if (age) {
var busPass = getBusPass(age); // might be null or undefined
if (busPass) {
canRideForFree = busPass.isValidForRoute('Weston');
}
}
canRideForFree = false;
`
Please excuse the messy var scoping and implicit any types in the above. Again, the neat thing about the caseOf method is that it forces you to consider the failure case - it's not always obvious if you're missing a branch of your if-else statement, until it blows up at runtime.
There are some convenience methods in Maybe:
`ts
user.getLikesCookies().defaulting(false); // Maybe
user.getLikesCookies().valueOr(false); // false
user.getLikesCookies().valueOrCompute(() => expensiveCalculation());
user.getLikesCookies().valueOrThrow(new Error());
// Maybe.just({ three: 3, hi: 'hi'})
Maybe.sequence
// Maybe.nothing
Maybe.sequence
`
`ts`
var canRideForFree = user.getAge() // either 42 or 'Information withheld' - type of Either
.bind(age => getBusPass(age)) // either busPass or 'Too young for a bus pass' - type of Either
.caseOf({
right: busPass => busPass.isValidForRoute('Weston'),
left: errorMessage => { console.log(errorMessage); return false; }
});
Somewhat contrived example of recording arithmetic operations:
`ts`
var is_true = Writer.writer(['Started with 0'], 0)
.bind(x => Writer.writer(['+ 8'], x + 8))
.bind(x => Writer.writer(['- 6', ' 8'], 8 (x - 6)))
.caseOf({
writer: (s, v) => v === 16 && s.join(', ') === 'Started with 0, + 8, - 6, * 8'
}));
The lift method takes a lambda, applies it to the wrapped value and calls the unit function of the monad on the result (e.g. for Maybe it calls just). Useful when you want to bind to a function that doesn't return a monad.
`ts``
var turns_out_to_be_true = Maybe.just(123)
.lift(n => n * 2)
.caseOf({
just: n => n === 246,
nothing: () => false
});
Note that for Maybe, if the lifted function returns null or undefined then it returns Nothing rather than wrapping a null in a Just, which is perverse.
These monads are the most obviously useful in JavaScript's world of unrestricted mutable state and side effects. I'm currently evaluating which other common monads offer enough benefit to be worth implementing in TypeScript.
#### Where's monad transformers?
Sorry. One day. But for the moment it's not practicable to do this without support for higher-kinded types.
#### Is it Fantasy Land conformant?
Yes - there are aliases for Fantasy Land interfaces of Functor and Monad.