Skip to content

Instantly share code, notes, and snippets.

@edysegura
Created February 29, 2016 04:32
Show Gist options
  • Save edysegura/cd230849584d1b9a53bc to your computer and use it in GitHub Desktop.
Save edysegura/cd230849584d1b9a53bc to your computer and use it in GitHub Desktop.
[JS] Functor
function stringFunctor(value, fn) {
var chars = value.split("")
return chars.map(function(char) {
return String.fromCharCode(fn(char.charCodeAt(0)))
}).join("")
}
function plus1(value) {
return value + 1
}
function minus1(value) {
return value - 1
}
[3,4].map(plus1) // = [4, 5]
stringFunctor("ABC", plus1) // returns "BCD"
stringFunctor(“XYZ”, minus1) // returns “RXY”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment