Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change folktale link to data.task #596

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dotnetCarpenter
Copy link
Contributor

I did not fully understand the Task example in chapter 8 and following the link to Folktale 2 and the new task implementation made me even more confused.

I tried to change the example to work with Folktale 2 but I do not know what to do with the error as it does not make sense in a cleanup operation.

Then I found data.task from the same author and with one minor change to the example everything worked as expected.

I added String because the data part of the fs.readFile callback is a Buffer in node version 14. I am not sure if was a string at the time of writing the example.

I think it is far easier to follow examples if they work with the current versions of contemporary software. Since data.task has been published as a separate module, it should be future proof with no apparent reason for change.

PS: My editor automatically deletes dangling spaces, therefore there is a bunch of seemingly non-changed lines. But I think it is in everyone's interest, to not have dangling spaces littering the files.

@dotnetCarpenter
Copy link
Contributor Author

In case it was not clear, my proposed change is:


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. Behold some example usage:

// -- Node readFile example ------------------------------------------

const Task = require('data.task');
const fs = require('fs');

// readFile :: String -> Task Error String
const readFile = filename => new Task((reject, result) => {
  fs.readFile(filename, (err, data) => (err ? reject(err) : result(data)));
});

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.')

@sigfriedCub1990
Copy link

sigfriedCub1990 commented May 3, 2022

I can see that this is a PR from last year but that code is again obsolete. Just like @dotnetCarpenter I like to run things on my computer and see them working, however this will not work with Folktale latest version, not to mention that the library is, unfortunately, discontinued.

In any case, for this to work we would need to modify it to:

const { task } = require("folktale/concurrency/task");
const { split, head, compose } = require("@mostly-adequate/support");
const fs = require("fs");

const readFile = (fileName) =>
  task((resolver) => {
    fs.readFile(fileName, (err, data) =>
      err 
        ? resolver.reject(err) 
        : resolver.resolve(data)
    );
  });

readFile("metamorphosis")
  .run()
  .future()
  .map(compose(split("\n"), String))
  .map(head)
  .map(console.log);

Hopefully this is helpful to anyone trying to run the example with the latest version of Folktale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants