I am trying to write a boolean currying function in javascript.
let s = "ajkjxa";
function isPresent(a) {
return function (b) {
if (b) {
return isPresent(s.includes(b) && s.includes(a));
} else {
return s.includes(a);
}
};
}
console.log(isPresent("a")("j")("x")());//true expected
console.log(isPresent("a")("j")("x")('b')());//false expected
I want isPresent function should return true if the passed arguments present is the given string else it should return false.
CodePudding user response:
A generic solution is to have the "accumulator" value passed in differently. The first call you do to isPresent
should already call the closure, and isPresent()
should also work.
function makePresenceChecker(string, found) {
return function(char) {
if (char == undefined)
return found;
else
return makePresenceChecker(string, found && string.includes(char));
};
}
const isPresent = makePresenceChecker("ajkjxa", true);
console.log(isPresent("a")("j")("x")()); // true
console.log(isPresent("a")("j")("x")('b')()); // false
You can also write that with an IIFE:
const string = "ajkjxa";
const isPresent = (function makePresenceChecker(found) {
return function(char) {
if (char == undefined)
return found;
else
return makePresenceChecker(found && string.includes(char));
};
})(true);
CodePudding user response:
I'm mostly answering this as a learning exercise. After much struggling I have something I know works. I've used different syntax and naming than your initial example, and I pass the initial string in a setup call.
It's possible this isn't strictly "currying" since we only need to aggregate the booleans. It didn't seem necessary to compile a list of function calls since the boolean results are sufficient.
As mentioned in comments, returning a function at the end is unnecessary at least in pure JS. In TypeScript that I wrote this in, I can't get the type to work this way. (The return type depends on the argument type, and you can't call a (boolean | function)
if that is what is returned.)
const startcurry = start => { // string
const results = []; // boolean[]
function inner(maybeNext) { // string?
if (maybeNext) {
const next = maybeNext;
results.push(start.includes(next));
return inner;
}
return results.every(x => x);
}
return inner;
};
const s = "ajkjxa";
console.log(startcurry(s)("a")("j")("x")()); // true expected
console.log(startcurry(s)("a")("j")("x")('b')()); // false expected
console.log(startcurry(s)("a")("j")("x")('k')()); // true expected
console.log(startcurry(s)("a")("j")("x")('k')('c')()); // false expected
console.log(startcurry(s)()); // true expected