{"action":"created","issue":{"url":"https://api.github.com/repos/MostlyAdequate/mostly-adequate-guide/issues/264","repository_url":"https://api.github.com/repos/MostlyAdequate/mostly-adequate-guide","labels_url":"https://api.github.com/repos/MostlyAdequate/mostly-adequate-guide/issues/264/labels{/name}","comments_url":"https://api.github.com/repos/MostlyAdequate/mostly-adequate-guide/issues/264/comments","events_url":"https://api.github.com/repos/MostlyAdequate/mostly-adequate-guide/issues/264/events","html_url":"https://github.com/MostlyAdequate/mostly-adequate-guide/issues/264","id":145288084,"number":264,"title":"Correct implementation of join for IO","user":{"login":"DanBurton","id":713692,"avatar_url":"https://avatars.githubusercontent.com/u/713692?v=3","gravatar_id":"","url":"https://api.github.com/users/DanBurton","html_url":"https://github.com/DanBurton","followers_url":"https://api.github.com/users/DanBurton/followers","following_url":"https://api.github.com/users/DanBurton/following{/other_user}","gists_url":"https://api.github.com/users/DanBurton/gists{/gist_id}","starred_url":"https://api.github.com/users/DanBurton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DanBurton/subscriptions","organizations_url":"https://api.github.com/users/DanBurton/orgs","repos_url":"https://api.github.com/users/DanBurton/repos","events_url":"https://api.github.com/users/DanBurton/events{/privacy}","received_events_url":"https://api.github.com/users/DanBurton/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-01T21:24:24Z","updated_at":"2016-04-03T07:41:56Z","closed_at":null,"body":"The provided implementation of `join` for `IO` is as follows:\r\n\r\n```javascript\r\nIO.prototype.join = function() {\r\n  return this.unsafePerformIO();\r\n}\r\n```\r\n\r\nThis is incorrect, because it runs the outer IO effect at \"composition time\" rather than \"run time\".\r\n\r\n```javascript\r\nvar example = new IO(function() {\r\n  console.log('effect happened');\r\n  return IO.of(3);\r\n});\r\n\r\nvar joinedExample = example.join();\r\n// prints 'effect happened'\r\n\r\njoinedExample.unsafePerformIO();\r\n// nothing is printed\r\njoinedExample.unsafePerformIO();\r\n// (again) nothing is printed\r\n```\r\n\r\nThe correct implementation would be something like the following:\r\n\r\n```javascript\r\nIO.prototype.join2 = function() {\r\n  var thiz = this;\r\n  return new IO(function() {\r\n    return thiz.unsafePerformIO().unsafePerformIO();\r\n  });\r\n};\r\n\r\nvar joinedExample2 = example.join2();\r\n// nothing is printed\r\n\r\njoinedExample2.unsafePerformIO();\r\n// prints 'effect happened'\r\njoinedExample2.unsafePerformIO();\r\n// (again) prints 'effect happened'\r\n```"},"comment":{"url":"https://api.github.com/repos/MostlyAdequate/mostly-adequate-guide/issues/comments/204899591","html_url":"https://github.com/MostlyAdequate/mostly-adequate-guide/issues/264#issuecomment-204899591","issue_url":"https://api.github.com/repos/MostlyAdequate/mostly-adequate-guide/issues/264","id":204899591,"user":{"login":"DanBurton","id":713692,"avatar_url":"https://avatars.githubusercontent.com/u/713692?v=3","gravatar_id":"","url":"https://api.github.com/users/DanBurton","html_url":"https://github.com/DanBurton","followers_url":"https://api.github.com/users/DanBurton/followers","following_url":"https://api.github.com/users/DanBurton/following{/other_user}","gists_url":"https://api.github.com/users/DanBurton/gists{/gist_id}","starred_url":"https://api.github.com/users/DanBurton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DanBurton/subscriptions","organizations_url":"https://api.github.com/users/DanBurton/orgs","repos_url":"https://api.github.com/users/DanBurton/repos","events_url":"https://api.github.com/users/DanBurton/events{/privacy}","received_events_url":"https://api.github.com/users/DanBurton/received_events","type":"User","site_admin":false},"created_at":"2016-04-03T07:41:56Z","updated_at":"2016-04-03T07:41:56Z","body":"I went ahead and opened a PR. However, the `this` aliasing doesn't quite fit in with the style of the rest of the book, so perhaps you can come up with a cleaner implementation."}}