Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda