fantasyland / fantasy-io

IO control structure.
37 stars 3 forks source link

Fantasy-IO does not comply to Fantasyland specs #2

Open nadameu opened 7 years ago

nadameu commented 7 years ago

According to the Fantasyland spec, v.ap(u.ap(a.map(f => g => x => f(g(x))))) must be equivalent to v.ap(u).ap(a) https://github.com/fantasyland/fantasy-land#apply .

I am using fantasy-io.js and trying to run the following tests:

const log = msg => x => {
  console.log(msg, x);
  return x;
};

const v = IO.of('v');
const u = IO.of(x => `u(${x})`);
const a = IO.of(x => `a(${x})`);

const testA = v.ap(u.ap(a.map(f => g => x => f(g(x)))))
  .map(log('testA = '))
  .unsafePerform();

const testB = v.ap(u).ap(a)
  .map(log('testB = '))
  .unsafePerform();

The result is: TypeError: f is not a function on the line that says const testA = v.ap(u.ap(a.map(f => g => x => f(g(x)))))

When I swap a and this in the body of the function IO.prototype.ap, the tests succeed:

testA =  a(u(v))
testB =  a(u(v))
safareli commented 7 years ago

This lib is outdated, it is supporting old version of fantasy-land

mwalkerwells commented 7 years ago

@nadameu What are you using for the IO monad now? End up writing something?

safareli commented 7 years ago

It would be better to update this lib then write new one PRs are welcome!

mwalkerwells commented 7 years ago

@safareli What are you using to model IO?

nadameu commented 7 years ago

@mwalkerwells I usually write my FL implementations from scratch, since I write mostly Greasemonkey scripts, which makes working with libraries (imports/exports) difficult.

Also, most implementations have a lot of methods I usually don't need, and I need my code to be lightweight.

I like this style:

const IO = performUnsafeIO => ({
    performUnsafeIO,
    ap: a => IO(() => a.performUnsafeIO()(performUnsafeIO())),
    chain: f => IO(() => f(performUnsafeIO()).performUnsafeIO()),
    map: f => IO(() => f(performUnsafeIO()))
});
IO.of = x => IO(() => x);
mwalkerwells commented 7 years ago

Ah, gotcha. I was also actually thinking of forking to be FL compliant. Any reason you haven't submitted a PR upstream?

nadameu commented 7 years ago

Pull request #3 submitted

mwalkerwells commented 7 years ago

Love it man 👍🏻👍🏻