Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Boolean Currying Javascript

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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!