akimichi / functionaljs Goto Github PK
View Code? Open in Web Editor NEW「関数型プログラミングの基礎」サンプルコード
Home Page: http://akimichi.github.io/functionaljs/
License: MIT License
「関数型プログラミングの基礎」サンプルコード
Home Page: http://akimichi.github.io/functionaljs/
License: MIT License
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)))
};
}
よろしくお願いします。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.