No. Function composition is
substr(indexOf(url))
= compose(substr, indexOf)(url)
what you need is an Applicative actually which features
substr(url, indexOf(url)) // actually substr(url)(indexOf(url))
= ap(substr, indexOf)(url)
Luckily, implementations for that exist already, let's go with
function ap(f, g) {
return function() {
return f.apply(this, arguments)(g.apply(this, arguments));
};
}
Next, we need a way to turn the methods of String.prototype
into functions, like they already exist in Firefox. We can use bindbind for that purpose, which also has the benefit of returning (partially) curried functions.
var substr = Function.prototype.bind.bind(String.prototype.substr);
var indexOf = Function.prototype.bind.bind(String.prototype.indexOf);
You would use them like indexOf("abcd")("c")
or substr("abcd")(2)
now. Last but not least we need to flip the arguments of indexOf
, so that we could use it like flippedIndexOf("c")("abcd")
(and more importantly, get the flippedIndexOf("c")
function on its own). So let's use
function flip(f) {
return function(b) {
return function(a) {
return f(a)(b);
};
};
}
Now, we can do what you initially wanted:
var c = ap(substr, flip(indexOf)("?"));
Looks good, and actually works!
But probably this is not worth all the hazzle. Just go with
function c(url) { return url.substr(url.indexOf('?')); }
which everybody understands even if he has no Computer Science degree :-)