Last active
March 29, 2016 03:40
-
-
Save kreja/e618f71b3bd2e281cc8a to your computer and use it in GitHub Desktop.
mostly-adequate-guide
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
// https://github.com/MostlyAdequate/mostly-adequate-guide | |
// https://llh911001.gitbooks.io/mostly-adequate-guide-chinese/content/ch5.html | |
// this is my practice while reading the book | |
var compose = function(){ | |
var args = arguments; | |
return function(x){ | |
var res = x; | |
for(var i=args.length-1; i>=0; i--){ | |
res = args[i](res); | |
} | |
return res; | |
}; | |
}; | |
var toUpperCase = function(str){ | |
return str.toUpperCase(); | |
}; | |
var getMatchFn = function(RegExp){ // it's a curry func! | |
return function(str){ | |
var res = str.match(RegExp); | |
return res && res[0] || 'not matched'; | |
}; | |
}; | |
var findHello = function(s){ | |
return getMatchFn(/hello/)(s); | |
}; | |
var strengthHello = compose(toUpperCase, findHello); | |
console.log( strengthHello('yo hello world') ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment