Reputation: 386
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.
Upvotes: 3
Views: 169
Reputation: 433
You can just change it so it checks for 'a' everytime but calls isPresent(b) recursively, if it exists
let s = "ajkjxa";
function isPresent(a, valid=true) {
return function (b) {
let isValid = valid && s.includes(a);
if (b) return isPresent(b, isValid)
return isValid
};
}
console.log(isPresent("a")("j")("x")());//true expected
console.log(isPresent("a")("j")("x")('b')());//false expected
console.log(isPresent("a")("g")("l")());//false expected
console.log(isPresent("a")("s")("b")("b")());//false expected
console.log(isPresent("a")("s")("l")("a")());//false expected
EDIT: Moved to a default parameters, less code and it works for the use cases in the comment.
Upvotes: -1
Reputation: 13198
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[] (could be boolean instead)
function inner(maybeNext) { // string?
if (maybeNext) {
const next = maybeNext; // string (just for naming rule)
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
console.log(startcurry(s)("b")("a")()); // false expected
Upvotes: -1
Reputation: 665185
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);
console.log(isPresent("a")("j")("x")()); // true
console.log(isPresent("a")("j")("x")('b')()); // false
Upvotes: 3