Skip to content

Instantly share code, notes, and snippets.

@tuxsudo
Last active March 22, 2016 20:26
Show Gist options
  • Save tuxsudo/7b2f00d5ef6a55abd7d2 to your computer and use it in GitHub Desktop.
Save tuxsudo/7b2f00d5ef6a55abd7d2 to your computer and use it in GitHub Desktop.
Functional Stuff
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));
}
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;
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();
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