Skip to content

Instantly share code, notes, and snippets.

@webbower
Created July 10, 2015 04:51
Show Gist options
  • Save webbower/27205c412a531b01112d to your computer and use it in GitHub Desktop.
Save webbower/27205c412a531b01112d to your computer and use it in GitHub Desktop.
Fun(ctional) squaring
var n = 10;
// Simple squaring function
function square(n) {
return n * n;
}
// Flip the first and second args of a function signature
function flip(fn) {
return function(a, b) {
return fn(b, a);
}
}
// Return the value
function identity(val) {
return val;
}
// Generate a dense array of undefined's
Array.dense = function dense(length) {
return Array.apply(null, Array(length));
};
// Functionally generate an array of numbers between start (1) and end
Array.range = function range(end, start) {
start = (start === undefined ? 1 : Number(start));
return Array.dense(start + end).slice(start, end).map(flip(identity));
};
console.log(
Array.range(n)
.map(square)
.join(' ')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment