diff --git a/ch08.md b/ch08.md index 648d21e1..aa525d20 100644 --- a/ch08.md +++ b/ch08.md @@ -13,7 +13,7 @@ class Container { constructor(x) { this.$value = x; } - + static of(x) { return new Container(x); } @@ -61,13 +61,13 @@ Container.prototype.map = function (f) { Why, it's just like Array's famous `map`, except we have `Container a` instead of `[a]`. And it works essentially the same way: ```js -Container.of(2).map(two => two + 2); +Container.of(2).map(two => two + 2); // Container(4) -Container.of('flamethrowers').map(s => s.toUpperCase()); +Container.of('flamethrowers').map(s => s.toUpperCase()); // Container('FLAMETHROWERS') -Container.of('bombs').map(append(' away')).map(prop('length')); +Container.of('bombs').map(append(' away')).map(prop('length')); // Container(10) ``` @@ -169,7 +169,7 @@ 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 +// updateLedger :: Account -> Account const updateLedger = account => account; // remainingBalance :: Account -> String @@ -182,7 +182,7 @@ const finishTransaction = compose(remainingBalance, updateLedger); // getTwenty :: Account -> Maybe(String) const getTwenty = compose(map(finishTransaction), withdraw(20)); -getTwenty({ balance: 200.00 }); +getTwenty({ balance: 200.00 }); // Just('Your balance is $180') getTwenty({ balance: 10.00 }); @@ -212,10 +212,10 @@ const maybe = curry((v, f, m) => { // getTwenty :: Account -> String const getTwenty = compose(maybe('You\'re broke!', finishTransaction), withdraw(20)); -getTwenty({ balance: 200.00 }); +getTwenty({ balance: 200.00 }); // 'Your balance is $180.00' -getTwenty({ balance: 10.00 }); +getTwenty({ balance: 10.00 }); // 'You\'re broke!' ``` @@ -272,10 +272,10 @@ const left = x => new Left(x); `Left` and `Right` are two subclasses of an abstract type we call `Either`. I've skipped the ceremony of creating the `Either` superclass as we won't ever use it, but it's good to be aware. Now then, there's nothing new here besides the two types. Let's see how they act: ```js -Either.of('rain').map(str => `b${str}`); +Either.of('rain').map(str => `b${str}`); // Right('brain') -left('rain').map(str => `It's gonna ${str}, better bring your umbrella!`); +left('rain').map(str => `It's gonna ${str}, better bring your umbrella!`); // Left('rain') Either.of({ host: 'localhost', port: 80 }).map(prop('host')); @@ -490,11 +490,12 @@ There, much better. Now our calling code becomes `findParam('searchTerm').unsafe Callbacks are the narrowing spiral staircase to hell. They are control flow as designed by M.C. Escher. With each nested callback squeezed in between the jungle gym of curly braces and parenthesis, they feel like limbo in an oubliette (how low can we go?!). I'm getting claustrophobic chills just thinking about them. Not to worry, we have a much better way of dealing with asynchronous code and it starts with an "F". -The internals are a bit too complicated to spill out all over the page here so we will use `Data.Task` (previously `Data.Future`) from Quildreen Motta's fantastic [Folktale](https://folktale.origamitower.com/). Behold some example usage: +The internals are a bit too complicated to spill out all over the page here so we will use `Data.Task` (previously `Data.Future`) from Quildreen Motta's fantastic [data.task](https://github.com/folktale/data.task). Behold some example usage: ```js // -- Node readFile example ------------------------------------------ +const Task = require('data.task'); const fs = require('fs'); // readFile :: String -> Task Error String @@ -502,7 +503,9 @@ const readFile = filename => new Task((reject, result) => { fs.readFile(filename, (err, data) => (err ? reject(err) : result(data))); }); -readFile('metamorphosis').map(split('\n')).map(head); +readFile('metamorphosis') + .map(compose(split('\n'), String)) // Convert Buffer to String before split + .map(head); // Task('One morning, as Gregor Samsa was waking up from anxious dreams, he discovered that // in bed he had been changed into a monstrous verminous bug.')