MostlyAdequate / mostly-adequate-guide

Mostly adequate guide to FP (in javascript)
Other
23.43k stars 1.87k forks source link

fix regression of IO.join in chapter 9 #631

Closed NANASHI0X74 closed 11 months ago

NANASHI0X74 commented 2 years ago

I noticed today that the IO.join() method seemed off, so I searched in issues and PRs whether someone else had spoken about this, subsequently I found this PR: https://github.com/MostlyAdequate/mostly-adequate-guide/pull/265 which had fixed the issue and this commit: https://github.com/MostlyAdequate/mostly-adequate-guide/commit/f1608d399c4045c0fe2b18824464c91e3afe9645 which reintroduced the error.

This PR is a cherry-pick of the old PR mentioned above

sevillaarvin commented 2 years ago

The current implementation is still out of date, I think:

IO.prototype.join = () => this.unsafePerformIO();

From: https://mostly-adequate.gitbook.io/mostly-adequate-guide/ch09

According to appendix_b (https://mostly-adequate.gitbook.io/mostly-adequate-guide/appendix_b), it should be:

IO.prototype.join = new IO(() => this.unsafePerformIO().unsafePerformIO());
I tested it myself and as far as I understand, it works: ```js #+begin_src js :noweb no-export <> class IO { constructor(fn) { this.unsafePerformIO = fn } static of(val) { return new IO(() => val) } map(fn) { return new IO(compose(fn, this.unsafePerformIO)) } join() { return new IO(() => this.unsafePerformIO().unsafePerformIO()) } } const x = IO.of(123) const y = IO.of(x) const z = y.join() return { x, // IO(123) xperf: x.unsafePerformIO(), // 123 y, // IO(IO(123)) yperf: y.unsafePerformIO(), // IO(123) z, // IO(123) zperf: z.unsafePerformIO(), // 123 } #+end_src #+RESULTS: : { : x: IO { unsafePerformIO: [Function (anonymous)] }, : xperf: 123, : y: IO { unsafePerformIO: [Function (anonymous)] }, : yperf: IO { unsafePerformIO: [Function (anonymous)] }, : z: IO { unsafePerformIO: [Function (anonymous)] }, : zperf: 123 : } ```