Skip to content

Instantly share code, notes, and snippets.

@upkarlidder
Created December 16, 2016 23:54
Show Gist options
  • Save upkarlidder/f3d38074eb7f52515f7954b916596b0e to your computer and use it in GitHub Desktop.
Save upkarlidder/f3d38074eb7f52515f7954b916596b0e to your computer and use it in GitHub Desktop.
//TRY 1
// const nextCharForNumberString = function(str){
// const trimmed = str.trim();
// const number = parseInt(trimmed);
// const nextNumber = number + 1;
// return String.fromCharCode(nextNumber);
// }
//TRY 2
// const nextCharForNumberString = function(str){
// return String.fromCharCode((parseInt(str.trim()) + 1));
// }
//TRY 3
const Box = function(x){
return {
map: function(f){
return Box(f(x))
},
inspect: function(){
return `Box(${x})`;
},
fold: function(f){
return f(x);
}
}
}
const nextCharForNumberString = function(str){
return Box(str)
.map(s => s.trim())
.map(s => parseInt(s))
.map(i => i + 1)
.map(i => String.fromCharCode(i))
.fold(c => c);
}
const result = nextCharForNumberString(' 64 ');
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment