Last active
August 30, 2018 19:46
-
-
Save angus-c/10994881 to your computer and use it in GitHub Desktop.
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
/* | |
sayIt accepts an unlimited number of chaining calls, each passing a word. When it's finally called | |
without arguments, all the passed words are printed in order | |
sayIt('have')('you')('got')('any')('fish')('fingers?')(); -> | |
"have you got any fish fingers?" | |
A regular implementaion would go sthg like this | |
function sayIt(firstWord) { | |
var words = []; | |
return (function reallySayIt(word) { | |
if (word) { | |
words.push(word); | |
return reallySayIt; | |
} else { | |
return words.join(' '); | |
} | |
})(firstWord); | |
} | |
During a bout of insomnia last night, I wondered if I could write it without any conditionals. | |
Here's what I got. | |
WARNING: IT'S MAD | |
*/ | |
var sayIt = function(word) { | |
var words = Array.prototype; | |
words.push(word); | |
sayIt.toString = function() { | |
try {return words.join(' ')} finally {words.splice(0, words.length);} | |
} | |
return sayIt; | |
} |
function sayIt() {
var words = [].slice.call(arguments, 0).join(" ")
try { words.length > arguments[0].length && arguments[1].length }
catch (e) { return words }
return function(word) {
return sayIt.call(null, words, word)
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bind All The Things