Giter VIP home page Giter VIP logo

functionaljs's People

Contributors

akimichi avatar moco7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

functionaljs's Issues

Wrong flatMap definition? (without world version)

You define IO.flatMap as below. (https://github.com/akimichi/functionaljs/blob/master/bin/io_without_world.js#L48-L53)

flatMap: (instanceA) => {
  var self = this;
  return (actionAB) => { // actionAB:: a -> IO b
    return self.unit(self.run(actionAB(self.run(instanceA))));
  };
}

But this definition evaluates self.run(actionAB(self.run(instanceA)) eagerly.
For example, the code below displays hello monad before executing IO.run(e).

// putStr :: String => IO[Unit]
const putStr = (s) => IO.flatMap(IO.unit(s))((s) => {
  console.log(s);
  return IO.unit();
});
const e = putStr('hello monad'); // display 'hello monad' at this point
IO.run(e); // shoud be displayed at this point

So I think the definition should be as below.

flatMap: (instanceA) => {
  const self = this;
  return (actionAB) => { // actionAB:: a -> IO b
    return () => self.run(actionAB(self.run(instanceA)))
  };
}

https://github.com/akimichi/functionaljs/blob/master/bin/io_without_world.js#L48-L53
にて、IO.flatMapは以下のように定義されています。

flatMap: (instanceA) => {
  var self = this;
  return (actionAB) => { // actionAB:: a -> IO b
    return self.unit(self.run(actionAB(self.run(instanceA))));
  };
}

しかし、この定義ではself.run(actionAB(self.run(instanceA))を正格評価してしまうと思います。
(正格評価という表現が適切か分かりませんが、IO.flatMap(instanceA)(actionAB)の時点で評価されます。)
例えば、以下のコードではIO.run(e)の実行前にhello monadが表示されます。

// putStr :: String => IO[Unit]
const putStr = (s) => IO.flatMap(IO.unit(s))((s) => {
  console.log(s);
  return IO.unit();
});
const e = putStr('hello monad'); // この時点で'hello monad'が表示されます
IO.run(e); // 実際には、この時点で表示されるべきかと思います

そのため、flatMapの定義としては以下のほうが適切かと思いますが如何でしょうか。

flatMap: (instanceA) => {
  const self = this;
  return (actionAB) => { // actionAB:: a -> IO b
    return () => self.run(actionAB(self.run(instanceA)))
  };
}

よろしくお願いします。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.