Created
February 29, 2016 04:32
-
-
Save edysegura/cd230849584d1b9a53bc to your computer and use it in GitHub Desktop.
[JS] Functor
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 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