Chapter 08: Tupperware
Maybe
implemented as Option
// withdraw :: Number -> Account -> Maybe(Account)
const withdraw = curry((amount, { balance }) =>
Maybe.of(balance >= amount ? { balance: balance - amount } : null));
// This function is hypothetical, not implemented here... nor anywhere else.
// updateLedger :: Account -> Account
const updateLedger = account => account;
// remainingBalance :: Account -> String
const remainingBalance = ({ balance }) => `Your balance is $${balance}`;
// finishTransaction :: Account -> String
const finishTransaction = compose(remainingBalance, updateLedger);
// getTwenty :: Account -> String
const getTwenty = compose(maybe('You\'re broke!', finishTransaction), withdraw(20));
getTwenty({ balance: 200.00 });
// 'Your balance is $180.00'
getTwenty({ balance: 10.00 });
// 'You\'re broke!'Either
IO
IO is implemented a little different from the above functors. Since IO is really just a "thunk", it's as it's one of the concepts which ts can support first class.
Task
In fp-ts, Task is defined as a () => Promise that will never fail (see the Practical Guide to fp-ts for an in-depth explanation).
This is inconsistent with the books use of Task. We will instead need to use TaskEither:
Last updated
Was this helpful?