Created
December 8, 2019 21:25
-
-
Save rauschma/9bcbc3b93727119aff65cc724a9749b9 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
function createStringBuilder() { | |
let str = ''; | |
return function self(key) { | |
switch (key) { | |
case 'append': | |
return (s) => { | |
str += s; | |
return self; | |
}; | |
case 'toString': | |
return () => str; | |
default: | |
throw new TypeError('Unknown key: ' + key); | |
} | |
}; | |
} | |
const sb = createStringBuilder(); | |
assert.equal( | |
sb('append')('Hello')('append')(' world!')('toString')(), | |
'Hello world!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment