Skip to content

Instantly share code, notes, and snippets.

@tuscen
Last active August 29, 2015 14:27
Show Gist options
  • Save tuscen/09adb3f1f529043ca1b9 to your computer and use it in GitHub Desktop.
Save tuscen/09adb3f1f529043ca1b9 to your computer and use it in GitHub Desktop.
let compose = (...fns) => x => fns.reverse().reduce((acc, f) => f(acc), x);
let map = f => functor => functor.map(f);
let log = function() {
console.log(this);
return this;
};
let Maybe = function(x) {
this.__value = x;
};
Maybe.of = x => 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));
};
let safeHead = xs => Maybe.of(xs[0]);
let streetName = compose(map(x => x.street), safeHead);
streetName([])::log();
streetName([{street: 'Shady Ln.', number: 4201}])::log();
'use strict';
var _context;
var compose = function compose() {
for (var _len = arguments.length, fns = Array(_len), _key = 0; _key < _len; _key++) {
fns[_key] = arguments[_key];
}
return function (x) {
return fns.reverse().reduce(function (acc, f) {
return f(acc);
}, x);
};
};
var map = function map(f) {
return function (functor) {
return functor.map(f);
};
};
var log = function log() {
console.log(this);
return this;
};
var Maybe = function Maybe(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));
};
var safeHead = function safeHead(xs) {
return Maybe.of(xs[0]);
};
var streetName = compose(map(function (x) {
return x.street;
}), safeHead);
(_context = streetName([]), log).call(_context);
(_context = streetName([{ street: 'Shady Ln.', number: 4201 }]), log).call(_context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment