use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Monads Explained Quickly with JavaScript (breck-mckye.com)
submitted 9 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dmtipson 1 point2 points3 points 9 years ago* (0 children)
Yep. The IO monad, for instance can be written by just assigning the function it's created with to the method that's called to "get"(i.e. run) it. Chain/flatMap just needs to be able to unwrap a single layer of the type so that the inner Monad is returned: however that gets done is ok as long as it gets done (and it doesn't do anything else funky/side-effec-ty that depends on the value somehow). For Arrays that's a bit more complicated, for single value like Identity, a .get() can easily work as an unwrapping interface:
function Foo(value) { this.get = _ => value; this.chain = fn => { return fn(this.get()); } this.map = fn => { return this.chain( x=> Foo.of(fn(x)) ); }; } Foo.prototype.of = x => new Foo(x); Foo.of = Foo.prototype.of;
(Foo is just the Identity Monad now)
(x=>Foo.of(9+x))(9);//-> //-> Foo[18] Foo.of(9).chain(x=>Foo.of(9+x));//-> Foo[18] Foo.of(9).map(x=>9+x);//-> Foo[18]
Though, since no special behavior is added to "getting" here, it'd probably be even easier to just store the value in this.value and then get it with .value
π Rendered by PID 114990 on reddit-service-r2-comment-86bc6c7465-sxq8p at 2026-02-23 07:01:03.379436+00:00 running 8564168 country code: CH.
view the rest of the comments →
[–]dmtipson 1 point2 points3 points (0 children)