Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda