Created
July 10, 2015 04:51
-
-
Save webbower/27205c412a531b01112d to your computer and use it in GitHub Desktop.
Fun(ctional) squaring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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