Last active
March 22, 2016 20:26
-
-
Save tuxsudo/7b2f00d5ef6a55abd7d2 to your computer and use it in GitHub Desktop.
Functional Stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Left = function(x) { | |
| this.__value = x; | |
| }; | |
| Left.of = function(x) { | |
| return new Left(x); | |
| }; | |
| Left.prototype.map = function(f) { | |
| return this; | |
| }; | |
| var Right = function(x) { | |
| this.__value = x; | |
| }; | |
| Right.of = function(x) { | |
| return new Right(x); | |
| }; | |
| Right.prototype.map = function(f) { | |
| return Right.of(f(this.__value)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Container = function(x) { this.__value = x; } | |
| Container.of = function(x) { | |
| return new Container(x); | |
| } | |
| Container.prototype.map = function(f) { | |
| return Container.of( | |
| f(this.__value) | |
| ); | |
| } | |
| // aplicative functor | |
| Container.prototype.ap = function(other_container) { | |
| return other_container.map(this.__value); | |
| } | |
| export default Container; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import compose from 'compose'; | |
| import prop from 'prop'; | |
| var IO = function(f) { | |
| this.execute = f; | |
| }; | |
| IO.of = function(x) { | |
| return new IO(function() { | |
| return x; | |
| }); | |
| }; | |
| IO.prototype.map = function(f) { | |
| return new IO(compose(f, this.execute)); | |
| }; | |
| // usage eg: | |
| // var winIO = new IO(()=>window); | |
| // winIO.map(prop('location')).execute(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Maybe = function(x) { | |
| this.__value = x; | |
| }; | |
| Maybe.of = function(x) { | |
| return new Maybe(x); | |
| }; | |
| Maybe.prototype.isNothing = function() { | |
| return (this.__value === null || this.__value === undefined); | |
| }; | |
| Maybe.prototype.map = function(f) { | |
| return this.isNothing() ? Maybe.of(null) : Maybe.of(f(this.__value)); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment