Skip to content

Instantly share code, notes, and snippets.

As we can see, for any n, the actual output will be [n, n], not [n, -1]

While I have no problem with hand-waving a bit in a mathematical argument for popular consumption, "as we can see" is really begging the question. If we could simply see this, then we would really not need to proof by contradiction at all. I think this should be spelled out.

For example, the cardinality of [0, 1, 2, 3, Infinity] is 4, the same as its length.

Oops. There seems to be a miscount here.

@CrossEye
CrossEye / permutations.js
Last active October 23, 2018 04:26
Find permutations of a list of (distinct) values. Uses Ramda
const R = require('ramda');
const permutations = (tokens, subperms = [[]]) =>
R.isEmpty(tokens) ?
subperms :
R.addIndex(R.chain)((token, idx) => permutations(
R.remove(idx, 1, tokens),
R.map(R.append(token), subperms)
), tokens);
@CrossEye
CrossEye / omega.js
Last active May 14, 2018 08:01
Functional compostion using fake operator overloading
// Based on a discussion with Michael Haufe:
// https://groups.google.com/group/jsmentors/browse_thread/thread/d028fb0041f93a27
// Not really recommended for anything but the fun of knowing it can be done!
var omega = function() {
var queue = [];
var valueOf = Function.prototype.valueOf;
Function.prototype.valueOf = function() {
queue.push(this);
return 1; // not needed now, but could be used later to distinguish operators.
@CrossEye
CrossEye / composeVariadic.js
Last active October 24, 2017 07:28
Variadic compose by multiple calls
var compose = function compose(f) {
var queue = f ? [f] : [];
var fn = function fn(g) {
if (arguments.length) {
queue.push(g);
return fn;
}
return function() {
var args = Array.prototype.slice.call(arguments);
queue.forEach(function(func) {
@CrossEye
CrossEye / namedCurry.js
Last active February 19, 2017 02:15
Named Curry
var R = require('./ramda');
// Discussion at https://github.com/ramda/ramda/issues/1258
var namedCurry = function(fn, argNames) {
// TODO: what if fn.length != argNames.length?
var f = R.curryN(fn.length, function() {
return fn.apply(this, arguments);
});
f['secret-sauce'] = {
@CrossEye
CrossEye / index.html
Created November 3, 2016 03:14 — forked from RubaXa/index.html
String#includes vs. String#indexOf vs. RegExp (http://jsbench.github.io/#a4612afd0cd26e911ee8) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>String#includes vs. String#indexOf vs. RegExp</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
// clean and pure:
function cons(x, y) {
return function(pick) {
return pick(x, y);
}
}
// does more stuff:
function cons(x, y) {
var fn = function(pick) {
@CrossEye
CrossEye / composePrototype.js
Last active June 16, 2016 08:48
Prototype composition
Function.prototype.compose = function(g) {
var fn = this;
return function() {
return fn.call(this, g.apply(this, arguments));
};
};
Function.prototype.pipe = function(g) {
var fn = this;
return function() {
@CrossEye
CrossEye / first.js
Last active June 16, 2016 08:48
An alternate form of functional composition.
var first = (function() {
var chainNames = ["then", "andThen", "next"];
var endChainNames = ["finally", "andFinally", "last"];
var chain = function(fn) {
var f1 = function(g) {
var func = function() {return g.call(this, fn.apply(this, arguments));};
chain(func);
return func;
};
var f2 = function(g) {
(function() {
var split = {}, intervalData,
timeComponent = {
seconds: function (split) {return split.seconds * 6;},
minutes: function (split) {return (split.minutes + (split.seconds / 60)) * 6;},
hours: function (split) {return (split.hours % 12 * 30 ) + split.minutes / 2;}
};
var SVG = (function() {
var SVG = function(config) {