Skip to content

Instantly share code, notes, and snippets.

@peel
Created February 26, 2015 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peel/dbd3cba74800ba7aa912 to your computer and use it in GitHub Desktop.
Save peel/dbd3cba74800ba7aa912 to your computer and use it in GitHub Desktop.
<script id="jsbin-javascript">
// object - contract/guard
var str = function (s) {
if (typeof s !== "string") {
throw new TypeError("Expected a string");
} else {
return s;
}
};
// morphism - guarded function
var repeat = function (s) {
s = str(s);
return s + s;
};
var typeOf = function (type) {
type = str(type);
return function (p) {
if (typeof p !== type) {
throw new TypeError("Expected a " + type);
} else {
return p;
}
};
};
var bool = typeOf("boolean");
var obj = typeOf("object");
var num = typeOf("number");
var undef = typeOf("undefined");
var any = function (x) {
return x;
};
var inc = function (x) {
x = num(x);
return num(x + 1);
};
var arr = function (a) {
if ({}.toString.call(a) !== "[object Array]") {
throw new TypeError("Expected an array");
} else {
return a;
}
};
console.log(arr([1,2,3]));
// functor - maps between categories (objects+morphisms)
var arrOf = function (c) {
return function (a) {
return arr(a).map(c);
};
};
console.log(arrOf(num)([1,2,3]));
console.log(arrOf(arrOf(num))([[1,2,3],[4,5]]));
// three-pointed set - maybe/option functor
var Maybe = function () {};
var None = function () {};
None.prototype = Object.create(Maybe.prototype);
None.prototype.toString = function () {
return "None";
};
var none = new None();
var Some = function (x) {
this.x = x;
};
Some.prototype = Object.create(Maybe.prototype);
Some.prototype.toString = function () {
return "Some(" + this.x + ")";
};
var some = function (x) {
return new Some(x);
};
//maybe functor (map from maybe to maybe)
var maybe = function (c) {
return function (m) {
if (m instanceof None) {
return m;
} else if (m instanceof Some) {
return some(c(m.x));
} else {
throw new TypeError("Expected None or Some(value)");
}
};
};
console.log("" + maybe(repeat)(none));
console.log("" + maybe(repeat)(some("piotr")));
Maybe.prototype.getOrElse = function (x) {
if (this instanceof Some) {
return this.x;
} else {
return x;
}
};
console.log("" + maybe(repeat)(none).getOrElse("peter"));
var arrOfUnit = function (c) {
return function (x) {
x = c(x);
return arrOf(c)([x]); // [x] - unit
};
};
console.log(arrOfUnit(num)(2));
Array.unit = function (x) {
return [x];
};
var maybeUnit = function (c) {
return function (x) {
x = c(x);
return maybe(c)(some(x));
};
};
Maybe.unit = some;
var arrOfFlatten = function (c) {
return function (aax) {
aax = arrOf(arrOf(c))(aax);
var result = [],
len = aax.length;
for (var i = 0; i < len; ++i) {
result = result.concat(aax[i]);
}
return result;
};
};
console.log(arrOfFlatten(num)([[1,2,3],[4,5]]));
//WTF?
Array.prototype.flatten = function (c) {
if (c === void 0) {
c = any;
}
return arrOfFlatten(c)(this);
};
var maybeFlatten = function (c) {
return function (mmx) {
mmx = maybe(maybe(c))(mmx);
var result = mmx;
if (result instanceof Some) {
result = result.x;
}
return maybe(c)(result);
};
};
Maybe.prototype.flatten = function (c) {
if (c === void 0) {
c = any;
}
return maybeFlatten(c)(this);
};
console.log([
[1, 2, 3],
[4, 5]
].flatten());
var twice = function (functor) {
return function (c) {
return functor(functor(c));
};
};
var once = function (functor) {
return functor;
};
var noTimes = function (functor) {
return function (c) {
return c;
};
};
var arrOfUnit = function (c) {
return function (x) {
x = noTimes(arrOf)(c)(x);
return once(arrOf)(c)([x]);
};
};
var maybeUnit = function (c) {
return function (x) {
x = noTimes(maybe)(c)(x);
return once(maybe)(c)(some(x));
};
};
var arrOfFlatten = function (c) {
return function (aax) {
aax = twice(arrOf)(c)(aax);
var result = [],
len = aax.length;
for (var i = 0; i < len; ++i) {
result = result.concat(aax[i]);
}
return once(arrOf)(c)(result);
};
};
var maybeFlatten = function (c) {
return function (mmx) {
mmx = twice(maybe)(c)(mmx);
var result = mmx;
if (result instanceof Some) {
result = result.x;
}
return once(maybe)(c)(result);
};
};
console.log(some(some(4)).flatten());
console.log(some(none).flatten());
console.log(none.flatten());
</script>
<script id="jsbin-source-javascript" type="text/javascript">// object - contract/guard
var str = function (s) {
if (typeof s !== "string") {
throw new TypeError("Expected a string");
} else {
return s;
}
};
// morphism - guarded function
var repeat = function (s) {
s = str(s);
return s + s;
};
var typeOf = function (type) {
type = str(type);
return function (p) {
if (typeof p !== type) {
throw new TypeError("Expected a " + type);
} else {
return p;
}
};
};
var bool = typeOf("boolean");
var obj = typeOf("object");
var num = typeOf("number");
var undef = typeOf("undefined");
var any = function (x) {
return x;
};
var inc = function (x) {
x = num(x);
return num(x + 1);
};
var arr = function (a) {
if ({}.toString.call(a) !== "[object Array]") {
throw new TypeError("Expected an array");
} else {
return a;
}
};
console.log(arr([1,2,3]));
// functor - maps between categories (objects+morphisms)
var arrOf = function (c) {
return function (a) {
return arr(a).map(c);
};
};
console.log(arrOf(num)([1,2,3]));
console.log(arrOf(arrOf(num))([[1,2,3],[4,5]]));
// three-pointed set - maybe/option functor
var Maybe = function () {};
var None = function () {};
None.prototype = Object.create(Maybe.prototype);
None.prototype.toString = function () {
return "None";
};
var none = new None();
var Some = function (x) {
this.x = x;
};
Some.prototype = Object.create(Maybe.prototype);
Some.prototype.toString = function () {
return "Some(" + this.x + ")";
};
var some = function (x) {
return new Some(x);
};
//maybe functor (map from maybe to maybe)
var maybe = function (c) {
return function (m) {
if (m instanceof None) {
return m;
} else if (m instanceof Some) {
return some(c(m.x));
} else {
throw new TypeError("Expected None or Some(value)");
}
};
};
console.log("" + maybe(repeat)(none));
console.log("" + maybe(repeat)(some("piotr")));
Maybe.prototype.getOrElse = function (x) {
if (this instanceof Some) {
return this.x;
} else {
return x;
}
};
console.log("" + maybe(repeat)(none).getOrElse("peter"));
var arrOfUnit = function (c) {
return function (x) {
x = c(x);
return arrOf(c)([x]); // [x] - unit
};
};
console.log(arrOfUnit(num)(2));
Array.unit = function (x) {
return [x];
};
var maybeUnit = function (c) {
return function (x) {
x = c(x);
return maybe(c)(some(x));
};
};
Maybe.unit = some;
var arrOfFlatten = function (c) {
return function (aax) {
aax = arrOf(arrOf(c))(aax);
var result = [],
len = aax.length;
for (var i = 0; i < len; ++i) {
result = result.concat(aax[i]);
}
return result;
};
};
console.log(arrOfFlatten(num)([[1,2,3],[4,5]]));
//WTF?
Array.prototype.flatten = function (c) {
if (c === void 0) {
c = any;
}
return arrOfFlatten(c)(this);
};
var maybeFlatten = function (c) {
return function (mmx) {
mmx = maybe(maybe(c))(mmx);
var result = mmx;
if (result instanceof Some) {
result = result.x;
}
return maybe(c)(result);
};
};
Maybe.prototype.flatten = function (c) {
if (c === void 0) {
c = any;
}
return maybeFlatten(c)(this);
};
console.log([
[1, 2, 3],
[4, 5]
].flatten());
var twice = function (functor) {
return function (c) {
return functor(functor(c));
};
};
var once = function (functor) {
return functor;
};
var noTimes = function (functor) {
return function (c) {
return c;
};
};
var arrOfUnit = function (c) {
return function (x) {
x = noTimes(arrOf)(c)(x);
return once(arrOf)(c)([x]);
};
};
var maybeUnit = function (c) {
return function (x) {
x = noTimes(maybe)(c)(x);
return once(maybe)(c)(some(x));
};
};
var arrOfFlatten = function (c) {
return function (aax) {
aax = twice(arrOf)(c)(aax);
var result = [],
len = aax.length;
for (var i = 0; i < len; ++i) {
result = result.concat(aax[i]);
}
return once(arrOf)(c)(result);
};
};
var maybeFlatten = function (c) {
return function (mmx) {
mmx = twice(maybe)(c)(mmx);
var result = mmx;
if (result instanceof Some) {
result = result.x;
}
return once(maybe)(c)(result);
};
};
console.log(some(some(4)).flatten());
console.log(some(none).flatten());
console.log(none.flatten());
</script>
// object - contract/guard
var str = function (s) {
if (typeof s !== "string") {
throw new TypeError("Expected a string");
} else {
return s;
}
};
// morphism - guarded function
var repeat = function (s) {
s = str(s);
return s + s;
};
var typeOf = function (type) {
type = str(type);
return function (p) {
if (typeof p !== type) {
throw new TypeError("Expected a " + type);
} else {
return p;
}
};
};
var bool = typeOf("boolean");
var obj = typeOf("object");
var num = typeOf("number");
var undef = typeOf("undefined");
var any = function (x) {
return x;
};
var inc = function (x) {
x = num(x);
return num(x + 1);
};
var arr = function (a) {
if ({}.toString.call(a) !== "[object Array]") {
throw new TypeError("Expected an array");
} else {
return a;
}
};
console.log(arr([1,2,3]));
// functor - maps between categories (objects+morphisms)
var arrOf = function (c) {
return function (a) {
return arr(a).map(c);
};
};
console.log(arrOf(num)([1,2,3]));
console.log(arrOf(arrOf(num))([[1,2,3],[4,5]]));
// three-pointed set - maybe/option functor
var Maybe = function () {};
var None = function () {};
None.prototype = Object.create(Maybe.prototype);
None.prototype.toString = function () {
return "None";
};
var none = new None();
var Some = function (x) {
this.x = x;
};
Some.prototype = Object.create(Maybe.prototype);
Some.prototype.toString = function () {
return "Some(" + this.x + ")";
};
var some = function (x) {
return new Some(x);
};
//maybe functor (map from maybe to maybe)
var maybe = function (c) {
return function (m) {
if (m instanceof None) {
return m;
} else if (m instanceof Some) {
return some(c(m.x));
} else {
throw new TypeError("Expected None or Some(value)");
}
};
};
console.log("" + maybe(repeat)(none));
console.log("" + maybe(repeat)(some("piotr")));
Maybe.prototype.getOrElse = function (x) {
if (this instanceof Some) {
return this.x;
} else {
return x;
}
};
console.log("" + maybe(repeat)(none).getOrElse("peter"));
var arrOfUnit = function (c) {
return function (x) {
x = c(x);
return arrOf(c)([x]); // [x] - unit
};
};
console.log(arrOfUnit(num)(2));
Array.unit = function (x) {
return [x];
};
var maybeUnit = function (c) {
return function (x) {
x = c(x);
return maybe(c)(some(x));
};
};
Maybe.unit = some;
var arrOfFlatten = function (c) {
return function (aax) {
aax = arrOf(arrOf(c))(aax);
var result = [],
len = aax.length;
for (var i = 0; i < len; ++i) {
result = result.concat(aax[i]);
}
return result;
};
};
console.log(arrOfFlatten(num)([[1,2,3],[4,5]]));
//WTF?
Array.prototype.flatten = function (c) {
if (c === void 0) {
c = any;
}
return arrOfFlatten(c)(this);
};
var maybeFlatten = function (c) {
return function (mmx) {
mmx = maybe(maybe(c))(mmx);
var result = mmx;
if (result instanceof Some) {
result = result.x;
}
return maybe(c)(result);
};
};
Maybe.prototype.flatten = function (c) {
if (c === void 0) {
c = any;
}
return maybeFlatten(c)(this);
};
console.log([
[1, 2, 3],
[4, 5]
].flatten());
var twice = function (functor) {
return function (c) {
return functor(functor(c));
};
};
var once = function (functor) {
return functor;
};
var noTimes = function (functor) {
return function (c) {
return c;
};
};
var arrOfUnit = function (c) {
return function (x) {
x = noTimes(arrOf)(c)(x);
return once(arrOf)(c)([x]);
};
};
var maybeUnit = function (c) {
return function (x) {
x = noTimes(maybe)(c)(x);
return once(maybe)(c)(some(x));
};
};
var arrOfFlatten = function (c) {
return function (aax) {
aax = twice(arrOf)(c)(aax);
var result = [],
len = aax.length;
for (var i = 0; i < len; ++i) {
result = result.concat(aax[i]);
}
return once(arrOf)(c)(result);
};
};
var maybeFlatten = function (c) {
return function (mmx) {
mmx = twice(maybe)(c)(mmx);
var result = mmx;
if (result instanceof Some) {
result = result.x;
}
return once(maybe)(c)(result);
};
};
console.log(some(some(4)).flatten());
console.log(some(none).flatten());
console.log(none.flatten());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment