From 5cb5788faccde640d2f4c2fa29118a6fe85d0cc4 Mon Sep 17 00:00:00 2001 From: dotnetCarpenter Date: Tue, 23 Feb 2021 19:49:58 +0100 Subject: [PATCH] fixing Waltz of the Types first example By using the improved `traverse` function from https://github.com/MostlyAdequate/mostly-adequate-guide/issues/581#issuecomment-668274810 and adding `toString` since the nodejs `fs.readFile` returns a Buffer and not a String. --- ch12.md | 4 ++-- support/index.js | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ch12.md b/ch12.md index 2da8c85c..903538a8 100644 --- a/ch12.md +++ b/ch12.md @@ -12,7 +12,7 @@ Let's get weird: // readFile :: FileName -> Task Error String // firstWords :: String -> String -const firstWords = compose(intercalate(' '), take(3), split(' ')); +const firstWords = compose(intercalate(' '), take(3), split(' '), toString); // tldr :: FileName -> Task Error String const tldr = compose(map(firstWords), readFile); @@ -149,7 +149,7 @@ Time to revisit and clean our initial examples. // readFile :: FileName -> Task Error String // firstWords :: String -> String -const firstWords = compose(intercalate(' '), take(3), split(' ')); +const firstWords = compose(intercalate(' '), take(3), split(' '), toString); // tldr :: FileName -> Task Error String const tldr = compose(map(firstWords), readFile); diff --git a/support/index.js b/support/index.js index 31d5ab23..0c285305 100644 --- a/support/index.js +++ b/support/index.js @@ -600,7 +600,11 @@ const toUpperCase = s => s.toUpperCase(); // traverse :: (Applicative f, Traversable t) => (a -> f a) -> (a -> f b) -> t a -> f (t b) -const traverse = curry((of, fn, f) => f.traverse(of, fn)); +const traverse = curry((of, fn, f) => + Array.isArray(f) + ? f.reduce((innerF, a) => fn(a).map(append).ap(innerF), of([])) + : f.traverse(of, fn) +); // unsafePerformIO :: IO a -> a